From 001f174cf5b06e8725d46a659ef5416c241b45dd Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Tue, 21 Sep 2010 14:23:57 -0400 Subject: [PATCH 1/1] Big rework of image.c and the image cache system. Added a lot of comments, simplified call graphs. Added full (not second-class) support for images coming from named sources (files, icon themes). RrImage holds an RrImageSet. RrImageSet holds a bunch of RrImagePic, which are different sizes of a logical image. RrImageSet objects can be merged if it is discovered they (will) share an RrImagePic. The RrImage objects are updated to use the new merged RrImageSet. --- obrender/image.c | 725 ++++++++++++++++++++++++++++++++++---------------- obrender/image.h | 4 - obrender/imagecache.c | 15 -- obrender/imagecache.h | 6 +- obrender/render.h | 95 ++++--- openbox/client.c | 61 ++--- openbox/menu.c | 14 +- 7 files changed, 587 insertions(+), 333 deletions(-) diff --git a/obrender/image.c b/obrender/image.c index bb65e3c..196d9d1 100644 --- a/obrender/image.c +++ b/obrender/image.c @@ -31,8 +31,25 @@ #define FLOOR(i) ((i) & (~0UL << FRACTION)) #define AVERAGE(a, b) (((((a) ^ (b)) & 0xfefefefeL) >> 1) + ((a) & (b))) -void RrImagePicInit(RrImagePic *pic, const gchar *name, - gint w, gint h, RrPixel32 *data) +/************************************************************************ + RrImagePic functions. + + RrImagePics are pictures that are grouped together into RrImageSets. Each + RrImagePic in the set has the same logical image inside it, but they are + of different sizes. An RrImagePic can be an original (which comes from some + outside source, such as an image file), or resized from some other RrImagePic + to meet the needs of the user. +**************************************************************************/ + + +/*! Set up an RrImagePic. + This does _not_ make a copy of the data. So the value of data must be + owned by the caller of this function, and not freed afterward. + This function does not allocate an RrImagePic, and can be used for setting + up a temporary RrImagePic on the stack. Such an object would then also + not be freed with RrImagePicFree. +*/ +static void RrImagePicInit(RrImagePic *pic, gint w, gint h, RrPixel32 *data) { gint i; @@ -42,49 +59,168 @@ void RrImagePicInit(RrImagePic *pic, const gchar *name, pic->sum = 0; for (i = w*h; i > 0; --i) pic->sum += *(data++); - pic->name = g_strdup(name); } +/*! Create a new RrImagePic from some picture data. + This makes a duplicate of the data. +*/ +static RrImagePic* RrImagePicNew(gint w, gint h, RrPixel32 *data) +{ + RrImagePic *pic; + + pic = g_slice_new(RrImagePic); + RrImagePicInit(pic, w, h, g_memdup(data, w*h*sizeof(RrPixel32))); + return pic; +} + + +/*! Destroy an RrImagePic. + This frees the RrImagePic object and everything inside it. +*/ static void RrImagePicFree(RrImagePic *pic) { if (pic) { g_free(pic->data); - g_free(pic->name); g_slice_free(RrImagePic, pic); } } -/*! Add a picture to an Image, that is, add another copy of the image at - another size. This may add it to the "originals" list or to the - "resized" list. */ -static void AddPicture(RrImage *self, RrImagePic ***list, gint *len, - RrImagePic *pic) +/************************************************************************ + RrImageSet functions. + + RrImageSets hold a group of pictures, each of which represent the same logical + image, but are physically different sizes. + At any time, it may be discovered that two different RrImageSets are actually + holding the same logical image. At that time, they would be merged. + An RrImageSet holds both original images which come from an outside source, + and resized images, which are generated when requests for a specific size are + made, and kept around in case they are request again. There is a maximum + number of resized images that an RrImageSet will keep around, however. + + Each RrImage points to a single RrImageSet, which keeps track of which + RrImages point to it. If two RrImageSets are merged, then the RrImages which + pointed to the two RrImageSets will all point at the resulting merged set. +**************************************************************************/ + + +/*! Free an RrImageSet and the stuff inside it. + This should only occur when there are no more RrImages pointing to the set. +*/ +static void RrImageSetFree(RrImageSet *self) { + GSList *it; gint i; + if (self) { + g_assert(self->images == NULL); + + /* remove all names associated with this RrImageSet */ + for (it = self->names; it; it = g_slist_next(it)) { + g_hash_table_remove(self->cache->name_table, it->data); + g_free(it->data); + } + g_slist_free(self->names); + + /* destroy the RrImagePic objects stored in the RrImageSet. they will + be keys in the cache to RrImageSet objects, so remove them from + the cache's pic_table as well. */ + for (i = 0; i < self->n_original; ++i) { + g_hash_table_remove(self->cache->pic_table, self->original[i]); + RrImagePicFree(self->original[i]); + } + g_free(self->original); + for (i = 0; i < self->n_resized; ++i) { + g_hash_table_remove(self->cache->pic_table, self->resized[i]); + RrImagePicFree(self->resized[i]); + } + g_free(self->resized); + + g_slice_free(RrImageSet, self); + } +} + +/*! Remove a picture from an RrImageSet as a given position. + @param set The RrImageSet to remove the picture from. + @param i The index of the picture in the RrImageSet in the list of + originals (if @original is TRUE), or in the list of resized pictures (if + @original is FALSE). + @param original TRUE if the picture is an original, FALSE if it is a resized + version of another picture in the RrImageSet. + */ +static void RrImageSetRemovePictureAt(RrImageSet *self, gint i, + gboolean original) +{ + RrImagePic ***list; + gint *len; + + if (original) { + list = &self->original; + len = &self->n_original; + } + else { + list = &self->resized; + len = &self->n_resized; + } + + g_assert(i >= 0 && i < *len); + + /* remove the picture data as a key in the cache */ + g_hash_table_remove(self->cache->pic_table, (*list)[i]); + + /* free the picture being removed */ + RrImagePicFree((*list)[i]); + + /* copy the elements after the removed one in the array forward one space + and shrink the array down one size */ + for (i = i+1; i < *len; ++i) + (*list)[i-1] = (*list)[i]; + --(*len); + *list = g_renew(RrImagePic*, *list, *len); +} + +/*! Add an RrImagePic to an RrImageSet. + The RrImagePic should _not_ exist in the image cache already. + Pictures are added to the front of the list, to maintain the ordering of + newest to oldest. +*/ +static void RrImageSetAddPicture(RrImageSet *self, RrImagePic *pic, + gboolean original) +{ + gint i; + RrImagePic ***list; + gint *len; + g_assert(pic->width > 0 && pic->height > 0); + g_assert(g_hash_table_lookup(self->cache->pic_table, pic) == NULL); + + /* choose which list in the RrImageSet to add the new picture to. */ + if (original) { + /* remove the resized picture of the same size if one exists */ + for (i = 0; i < self->n_resized; ++i) + if (self->resized[i]->width == pic->width || + self->resized[i]->height == pic->height) + { + RrImageSetRemovePictureAt(self, i, FALSE); + break; + } - if (pic->name) - g_assert(!g_hash_table_lookup(self->cache->name_table, pic->name)); - else - g_assert(!g_hash_table_lookup(self->cache->pic_table, pic)); + list = &self->original; + len = &self->n_original; + } + else { + list = &self->resized; + len = &self->n_resized; + } - /* grow the list */ + /* grow the list by one spot, shift everything down one, and insert the new + picture at the front of the list */ *list = g_renew(RrImagePic*, *list, ++*len); - - /* move everything else down one */ for (i = *len-1; i > 0; --i) (*list)[i] = (*list)[i-1]; - - /* set the new picture up at the front of the list */ (*list)[0] = pic; - /* add the name or the picture as a key to point to this image in the - cache */ - if (pic->name) - g_hash_table_insert(self->cache->name_table, pic->name, self); - else - g_hash_table_insert(self->cache->pic_table, (*list)[0], self); + /* add the picture as a key to point to this image in the cache */ + g_hash_table_insert(self->cache->pic_table, (*list)[0], self); /* #ifdef DEBUG @@ -96,42 +232,306 @@ static void AddPicture(RrImage *self, RrImagePic ***list, gint *len, */ } -/*! Remove a picture from an Image. This may remove it from the "originals" - list or the "resized" list. */ -static void RemovePicture(RrImage *self, RrImagePic ***list, - gint i, gint *len) +/*! Merges two image sets, destroying one, and returning the other. */ +RrImageSet* RrImageSetMergeSets(RrImageSet *b, RrImageSet *a) +{ + gint a_i, b_i, merged_i; + RrImagePic **original, **resized; + gint n_original, n_resized, tmp; + GSList *it; + + const gint max_resized = a->cache->max_resized_saved; + + if (!a) + return b; + if (!b) + return a; + if (a == b) + return b; + + /* the original and resized picture lists in an RrImageSet are kept ordered + as newest to oldest. we don't have timestamps for them, so we cannot + preserve this in the merged RrImageSet exactly. a decent approximation, + i think, is to add them in alternating order (one from a, one from b, + repeat). this way, the newest from each will be near the front at + least, and in the resized list, when we drop an old picture, we will + not always only drop from a or b only, but from each of them equally (or + from whichever has more resized pictures. + */ + + g_assert(b->cache == a->cache); + + a_i = b_i = merged_i = 0; + n_original = a->n_original + b->n_original; + original = g_new(RrImagePic*, n_original); + while (merged_i < n_original) { + if (a_i < a->n_original) + original[merged_i++] = a->original[a_i++]; + if (b_i < b->n_original) + original[merged_i++] = b->original[b_i++]; + } + + a_i = b_i = merged_i = 0; + n_resized = MIN(max_resized, a->n_resized + b->n_resized); + resized = g_new(RrImagePic*, n_resized); + while (merged_i < n_resized) { + if (a_i < a->n_resized) + resized[merged_i++] = a->resized[a_i++]; + if (b_i < b->n_resized && merged_i < n_resized) + resized[merged_i++] = b->resized[b_i++]; + } + + /* if there are any RrImagePic objects left over in a->resized or + b->resized, they need to be disposed of, and removed from the cache. + + updates the size of the list, as we want to remember which pointers + were merged from which list (and don't want to remember the ones we + did not merge and have freed). + */ + tmp = a_i; + for (; a_i < a->n_resized; ++a_i) { + g_hash_table_remove(a->cache->pic_table, a->resized[a_i]); + RrImagePicFree(a->resized[a_i]); + } + a->n_resized = tmp; + + tmp = b_i; + for (; b_i < b->n_resized; ++b_i) { + g_hash_table_remove(a->cache->pic_table, b->resized[b_i]); + RrImagePicFree(b->resized[b_i]); + } + b->n_resized = tmp; + + /* we will use the a object as the merge destination, so things in b will + be moving. + + the cache's name_table will point to b for all the names in b->names, + so these need to be updated to point at a instead. + also, the cache's pic_table will point to b for all the pictures in b, + so these need to be updated to point at a as well. + + any RrImage objects that were using b should now use a instead. + + the names and images will be all moved into a, and the merged picture + lists will be placed in a. the pictures in a and b are moved to new + arrays, so the arrays in a and b need to be freed explicitly (the + RrImageSetFree function would free the picture data too which we do not + want here). then b can be freed. + */ + + for (it = b->names; it; it = g_slist_next(it)) + g_hash_table_insert(a->cache->name_table, it->data, a); + for (b_i = 0; b_i < b->n_original; ++b_i) + g_hash_table_insert(a->cache->pic_table, b->original[b_i], a); + for (b_i = 0; b_i < b->n_resized; ++b_i) + g_hash_table_insert(a->cache->pic_table, b->resized[b_i], a); + + for (it = b->images; it; it = g_slist_next(it)) + ((RrImage*)it->data)->set = a; + + a->images = g_slist_concat(a->images, b->images); + b->images = NULL; + a->names = g_slist_concat(a->names, b->names); + b->names = NULL; + + a->n_original = a->n_resized = 0; + g_free(a->original); + g_free(a->resized); + a->original = a->resized = NULL; + b->n_original = b->n_resized = 0; + g_free(b->original); + g_free(b->resized); + b->original = b->resized = NULL; + + a->n_original = n_original; + a->original = original; + a->n_resized = n_resized; + a->resized = resized; + + RrImageSetFree(b); + + return a; +} + +static void RrImageSetAddName(RrImageSet *set, const gchar *name) +{ + gchar *n; + + n = g_strdup(name); + set->names = g_slist_prepend(set->names, n); + + /* add the new name to the hash table */ + g_assert(g_hash_table_lookup(set->cache->name_table, n) == NULL); + g_hash_table_insert(set->cache->name_table, n, set); +} + + +/************************************************************************ + RrImage functions. +**************************************************************************/ + + +void RrImageRef(RrImage *self) { - gint j; + ++self->ref; +} +void RrImageUnref(RrImage *self) +{ + if (self && --self->ref == 0) { + RrImageSet *set; /* #ifdef DEBUG - g_debug("Removing %s picture from the cache:\n " - "Image 0x%lx, w %d h %d Hash %u", - (*list == self->original ? "ORIGINAL" : "RESIZED"), - (gulong)self, (*list)[i]->width, (*list)[i]->height, - RrImagePicHash((*list)[i])); + g_debug("Refcount to 0, removing ALL pictures from the cache:\n " + "Image 0x%lx", (gulong)self); #endif */ + if (self->destroy_func) + self->destroy_func(self, self->destroy_data); - /* remove the name or picture as a key in the cache */ - if ((*list)[i]->name) - g_hash_table_remove(self->cache->name_table, (*list)[i]->name); - else - g_hash_table_remove(self->cache->pic_table, (*list)[i]); + set = self->set; + set->images = g_slist_remove(set->images, self); - /* free the picture */ - RrImagePicFree((*list)[i]); - /* shift everything down one */ - for (j = i; j < *len-1; ++j) - (*list)[j] = (*list)[j+1]; - /* shrink the list */ - *list = g_renew(RrImagePic*, *list, --*len); + /* free the set as well if there are no images pointing to it */ + if (!set->images) + RrImageSetFree(set); + g_slice_free(RrImage, self); + } +} + +/*! Set function that will be called just before RrImage is destroyed. */ +void RrImageSetDestroyFunc(RrImage *self, RrImageDestroyFunc func, + gpointer data) +{ + self->destroy_func = func; + self->destroy_data = data; +} + +void RrImageAddFromData(RrImage *self, RrPixel32 *data, gint w, gint h) +{ + RrImagePic pic, *ppic; + RrImageSet *set; + + g_return_if_fail(self != NULL); + g_return_if_fail(data != NULL); + g_return_if_fail(w > 0 && h > 0); + + RrImagePicInit(&pic, w, h, data); + set = g_hash_table_lookup(self->set->cache->pic_table, &pic); + if (set) + self->set = RrImageSetMergeSets(self->set, set); + else { + ppic = RrImagePicNew(w, h, data); + RrImageSetAddPicture(self->set, ppic, TRUE); + } +} + +RrImage* RrImageNewFromData(RrImageCache *cache, RrPixel32 *data, + gint w, gint h) +{ + RrImagePic pic, *ppic; + RrImage *self; + RrImageSet *set; + + g_return_val_if_fail(cache != NULL, NULL); + g_return_val_if_fail(data != NULL, NULL); + g_return_val_if_fail(w > 0 && h > 0, NULL); + + /* finds a picture in the cache, if it is already in there, and use the + RrImageSet the picture lives in. */ + RrImagePicInit(&pic, w, h, data); + set = g_hash_table_lookup(cache->pic_table, &pic); + if (set) { + self = set->images->data; /* just grab any RrImage from the list */ + RrImageRef(self); + return self; + } + + /* the image does not exist in any RrImageSet in the cache, so make + a new RrImageSet, and a new RrImage that points to it, and place the + new image inside the new RrImageSet */ + + self = g_slice_new0(RrImage); + self->ref = 1; + self->set = g_slice_new0(RrImageSet); + self->set->cache = cache; + self->set->images = g_slist_append(self->set->images, self); + + ppic = RrImagePicNew(w, h, data); + RrImageSetAddPicture(self->set, ppic, TRUE); + + return self; +} + +RrImage* RrImageNewFromName(RrImageCache *cache, const gchar *name) +{ +#ifndef USE_IMLIB2 + return NULL; +#else + RrImage *self; + RrImageSet *set; + Imlib_Image img; + gint w, h; + RrPixel32 *data; + gchar *path; + + g_return_val_if_fail(cache != NULL, NULL); + g_return_val_if_fail(name != NULL, NULL); + + set = g_hash_table_lookup(cache->name_table, name); + if (set) { + self = set->images->data; + RrImageRef(self); + return self; + } + + /* XXX find the path via freedesktop icon spec (use obt) ! */ + path = g_strdup(name); + + if (!(img = imlib_load_image(path))) + g_message("Cannot load image \"%s\" from file \"%s\"", name, path); + g_free(path); + + if (!img) + return NULL; + + /* Get data and dimensions of the image. + + WARNING: This stuff is NOT threadsafe !! + */ + imlib_context_set_image(img); + data = imlib_image_get_data_for_reading_only(); + w = imlib_image_get_width(); + h = imlib_image_get_height(); + + /* get an RrImage that contains an RrImageSet with this picture in it. + the RrImage might be new, or reused if the picture was already in the + cache. + + either way, we get back an RrImageSet (via the RrImage), and we must add + the name to that RrImageSet. because of the check above, we know that + there is no RrImageSet in the cache which already has the given name + asosciated with it. + */ + + self = RrImageNewFromData(cache, data, w, h); + RrImageSetAddName(self->set, name); + + imlib_free_image(); + return self; +#endif } +/************************************************************************ + Image drawing and resizing operations. +**************************************************************************/ + /*! Given a picture in RGBA format, of a specified size, resize it to the new requested size (but keep its aspect ratio). If the image does not need to be resized (it is already the right size) then this returns NULL. Otherwise it returns a newly allocated RrImagePic with the resized picture inside it + @return Returns a newly allocated RrImagePic object with a new version of the + image in the requested size (keeping aspect ratio). */ static RrImagePic* ResizeImage(RrPixel32 *src, gulong srcW, gulong srcH, @@ -144,11 +544,10 @@ static RrImagePic* ResizeImage(RrPixel32 *src, gulong ratioX, ratioY; gulong aspectW, aspectH; - /* XXX should these variables be ensured to not be zero in the callers? */ - srcW = srcW ? srcW : 1; - srcH = srcH ? srcH : 1; - dstW = dstW ? dstW : 1; - dstH = dstH ? dstH : 1; + g_assert(srcW > 0); + g_assert(srcH > 0); + g_assert(dstW > 0); + g_assert(dstH > 0); /* keep the aspect ratio */ aspectW = dstW; @@ -236,7 +635,7 @@ static RrImagePic* ResizeImage(RrPixel32 *src, } pic = g_slice_new(RrImagePic); - RrImagePicInit(pic, NULL, dstW, dstH, dststart); + RrImagePicInit(pic, dstW, dstH, dststart); return pic; } @@ -328,142 +727,6 @@ void RrImageDrawRGBA(RrPixel32 *target, RrTextureRGBA *rgba, rgba->alpha, area); } -/*! Create a new RrImage, which is linked to an image cache */ -RrImage* RrImageNew(RrImageCache *cache) -{ - RrImage *self; - - g_assert(cache != NULL); - - self = g_slice_new0(RrImage); - self->ref = 1; - self->cache = cache; - return self; -} - -/*! Set function that will be called just before RrImage is destroyed. */ -void RrImageSetDestroyFunc(RrImage *image, RrImageDestroyFunc func, - gpointer data) -{ - image->destroy_func = func; - image->destroy_data = data; -} - -void RrImageRef(RrImage *self) -{ - ++self->ref; -} - -void RrImageUnref(RrImage *self) -{ - if (self && --self->ref == 0) { -/* -#ifdef DEBUG - g_debug("Refcount to 0, removing ALL pictures from the cache:\n " - "Image 0x%lx", (gulong)self); -#endif -*/ - if (self->destroy_func) - self->destroy_func(self, self->destroy_data); - while (self->n_original > 0) - RemovePicture(self, &self->original, 0, &self->n_original); - while (self->n_resized > 0) - RemovePicture(self, &self->resized, 0, &self->n_resized); - g_slice_free(RrImage, self); - } -} - -static void AddPictureFromData(RrImage *self, const char *name, - const RrPixel32 *data, gint w, gint h) -{ - gint i; - RrImagePic *pic; - - /* make sure we don't already have this size.. */ - for (i = 0; i < self->n_original; ++i) - if (self->original[i]->width == w && self->original[i]->height == h) { -/* -#ifdef DEBUG - g_debug("Found duplicate ORIGINAL image:\n " - "Image 0x%lx, w %d h %d", (gulong)self, w, h); -#endif -*/ - return; - } - - /* remove any resized pictures of this same size */ - for (i = 0; i < self->n_resized; ++i) - if (self->resized[i]->width == w || self->resized[i]->height == h) { - RemovePicture(self, &self->resized, i, &self->n_resized); - break; - } - - /* add the new picture */ - pic = g_slice_new(RrImagePic); - RrImagePicInit(pic, name, w, h, g_memdup(data, w*h*sizeof(RrPixel32))); - AddPicture(self, &self->original, &self->n_original, pic); -} - -gboolean RrImageAddPictureName(RrImage *self, const gchar *name) -{ -#ifdef USE_IMLIB2 - Imlib_Image img; - gint w, h; - RrPixel32 *data; - gchar *path; - - /* XXX find the path via freedesktop icon spec (use obt) ! */ - path = g_strdup(name); - - if (!(img = imlib_load_image(path))) - g_message("Cannot load image \"%s\" from file \"%s\"", name, path); - g_free(path); - - if (!img) - return FALSE; /* failed to load it */ - - /* Get data and dimensions of the image. - - WARNING: This stuff is NOT threadsafe !! - */ - imlib_context_set_image(img); - data = imlib_image_get_data_for_reading_only(); - w = imlib_image_get_width(); - h = imlib_image_get_height(); - - /* add it to the RrImage, and set its name */ - AddPictureFromData(self, name, data, w, h); - - imlib_free_image(); - return TRUE; -#else - return FALSE; -#endif -} - -/*! Add a new picture with the given RGBA pixel data and dimensions into the - RrImage. This adds an "original" picture to the image. -*/ -void RrImageAddPicture(RrImage *self, const RrPixel32 *data, gint w, gint h) -{ - AddPictureFromData(self, NULL, data, w, h); -} - -/*! Remove the picture from the RrImage which has the given dimensions. This - removes an "original" picture from the image. -*/ -void RrImageRemovePicture(RrImage *self, gint w, gint h) -{ - gint i; - - /* remove any resized pictures of this same size */ - for (i = 0; i < self->n_original; ++i) - if (self->original[i]->width == w && self->original[i]->height == h) { - RemovePicture(self, &self->original, i, &self->n_original); - break; - } -} - /*! Draw an RrImage texture into a target pixel buffer. If the RrImage does not contain a picture of the appropriate size, then one of its "original" pictures will be resized and used (and stored in the RrImage as a "resized" @@ -475,65 +738,68 @@ void RrImageDrawImage(RrPixel32 *target, RrTextureImage *img, { gint i, min_diff, min_i, min_aspect_diff, min_aspect_i; RrImage *self; + RrImageSet *set; RrImagePic *pic; gboolean free_pic; self = img->image; + set = self->set; pic = NULL; free_pic = FALSE; /* is there an original of this size? (only the larger of w or h has to be right cuz we maintain aspect ratios) */ - for (i = 0; i < self->n_original; ++i) - if ((self->original[i]->width >= self->original[i]->height && - self->original[i]->width == area->width) || - (self->original[i]->width <= self->original[i]->height && - self->original[i]->height == area->height)) + for (i = 0; i < set->n_original; ++i) + if ((set->original[i]->width >= set->original[i]->height && + set->original[i]->width == area->width) || + (set->original[i]->width <= set->original[i]->height && + set->original[i]->height == area->height)) { - pic = self->original[i]; + pic = set->original[i]; break; } /* is there a resize of this size? */ - for (i = 0; i < self->n_resized; ++i) - if ((self->resized[i]->width >= self->resized[i]->height && - self->resized[i]->width == area->width) || - (self->resized[i]->width <= self->resized[i]->height && - self->resized[i]->height == area->height)) + for (i = 0; i < set->n_resized; ++i) + if ((set->resized[i]->width >= set->resized[i]->height && + set->resized[i]->width == area->width) || + (set->resized[i]->width <= set->resized[i]->height && + set->resized[i]->height == area->height)) { gint j; RrImagePic *saved; /* save the selected one */ - saved = self->resized[i]; + saved = set->resized[i]; /* shift all the others down */ for (j = i; j > 0; --j) - self->resized[j] = self->resized[j-1]; + set->resized[j] = set->resized[j-1]; /* and move the selected one to the top of the list */ - self->resized[0] = saved; + set->resized[0] = saved; - pic = self->resized[0]; + pic = set->resized[0]; break; } if (!pic) { gdouble aspect; + RrImageSet *cache_set; /* find an original with a close size */ min_diff = min_aspect_diff = -1; min_i = min_aspect_i = 0; aspect = ((gdouble)area->width) / area->height; - for (i = 0; i < self->n_original; ++i) { + for (i = 0; i < set->n_original; ++i) { gint diff; gint wdiff, hdiff; gdouble myasp; /* our size difference metric.. */ - wdiff = self->original[i]->width - area->width; + wdiff = set->original[i]->width - area->width; if (wdiff < 0) wdiff *= 2; /* prefer scaling down than up */ - hdiff = self->original[i]->height - area->height; + hdiff = set->original[i]->height - area->height; if (hdiff < 0) hdiff *= 2; /* prefer scaling down than up */ diff = (wdiff * wdiff) + (hdiff * hdiff); @@ -544,8 +810,8 @@ void RrImageDrawImage(RrPixel32 *target, RrTextureImage *img, } /* and also find the smallest difference with the same aspect ratio (and prefer this one) */ - myasp = ((gdouble)self->original[i]->width) / - self->original[i]->height; + myasp = ((gdouble)set->original[i]->width) / + set->original[i]->height; if (ABS(aspect - myasp) < 0.0000001 && (min_aspect_diff < 0 || diff < min_aspect_diff)) { @@ -559,24 +825,39 @@ void RrImageDrawImage(RrPixel32 *target, RrTextureImage *img, min_i = min_aspect_i; /* resize the original to the given area */ - pic = ResizeImage(self->original[min_i]->data, - self->original[min_i]->width, - self->original[min_i]->height, + pic = ResizeImage(set->original[min_i]->data, + set->original[min_i]->width, + set->original[min_i]->height, area->width, area->height); - /* add the resized image to the image, as the first in the resized - list */ - if (self->n_resized >= self->cache->max_resized_saved) - /* remove the last one (last used one) */ - RemovePicture(self, &self->resized, self->n_resized - 1, - &self->n_resized); - if (self->cache->max_resized_saved) - /* add it to the top of the resized list */ - AddPicture(self, &self->resized, &self->n_resized, pic); - else - free_pic = TRUE; /* don't leak mem! */ + /* is it already in the cache ? */ + cache_set = g_hash_table_lookup(set->cache->pic_table, pic); + if (cache_set) { + /* merge this set with the one found in the cache - they are + apparently the same image ! then next time we won't have to do + this resizing, we will use the cache_set's pic instead. */ + set = RrImageSetMergeSets(set, cache_set); + free_pic = TRUE; + } + else { + /* add the resized image to the image, as the first in the resized + list */ + while (set->n_resized >= set->cache->max_resized_saved) + /* remove the last one (last used one) to make space for + adding our resized picture */ + RrImageSetRemovePictureAt(set, set->n_resized-1, FALSE); + if (set->cache->max_resized_saved) + /* add it to the resized list */ + RrImageSetAddPicture(set, pic, FALSE); + else + free_pic = TRUE; /* don't leak mem! */ + } } + /* The RrImageSet may have changed if we merged it with another, so the + RrImage object needs to be updated to use the new merged RrImageSet. */ + self->set = set; + g_assert(pic != NULL); DrawRGBA(target, target_w, target_h, diff --git a/obrender/image.h b/obrender/image.h index 6e4a50e..28f29c2 100644 --- a/obrender/image.h +++ b/obrender/image.h @@ -22,10 +22,6 @@ #include "render.h" #include "geom.h" -/*! Initialize an RrImagePicture to the specified dimensions and pixel data */ -void RrImagePicInit(RrImagePic *pic, const gchar *path, - gint w, gint h, RrPixel32 *data); - void RrImageDrawImage(RrPixel32 *target, RrTextureImage *img, gint target_w, gint target_h, RrRect *area); diff --git a/obrender/imagecache.c b/obrender/imagecache.c index ad1183e..909d874 100644 --- a/obrender/imagecache.c +++ b/obrender/imagecache.c @@ -58,21 +58,6 @@ void RrImageCacheUnref(RrImageCache *self) } } -RrImage* RrImageCacheFindName(RrImageCache *self, const gchar *name) -{ - return g_hash_table_lookup(self->name_table, name); -} - -/*! Finds an image in the cache, if it is already in there */ -RrImage* RrImageCacheFind(RrImageCache *self, - RrPixel32 *data, gint w, gint h) -{ - RrImagePic pic; - - RrImagePicInit(&pic, NULL, w, h, data); - return g_hash_table_lookup(self->pic_table, &pic); -} - #define hashsize(n) ((RrPixel32)1<<(n)) #define hashmask(n) (hashsize(n)-1) #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) diff --git a/obrender/imagecache.h b/obrender/imagecache.h index 9baf34b..f9f5e4d 100644 --- a/obrender/imagecache.h +++ b/obrender/imagecache.h @@ -45,13 +45,13 @@ struct _RrImageCache { */ gint max_resized_saved; - /*! A hash table of images in the cache that don't have a file name + /*! A hash table of image sets in the cache that don't have a file name attached to them, with their key being a hash of the contents of the image. */ GHashTable *pic_table; - /*! Used to find out if an image file has already been loaded. - Provides a quick file_name -> RrImage lookup. */ + /*! Used to find out if an image file has already been loaded into an + image set. Provides a quick file_name -> RrImageSet lookup. */ GHashTable *name_table; }; diff --git a/obrender/render.h b/obrender/render.h index 37ffa55..d7066e2 100644 --- a/obrender/render.h +++ b/obrender/render.h @@ -44,10 +44,11 @@ typedef struct _RrPixmapMask RrPixmapMask; typedef struct _RrInstance RrInstance; typedef struct _RrColor RrColor; typedef struct _RrImage RrImage; +typedef struct _RrImageSet RrImageSet; typedef struct _RrImagePic RrImagePic; typedef struct _RrImageCache RrImageCache; -typedef guint32 RrPixel32; +typedef guint32 RrPixel32; /* RGBA format */ typedef guint16 RrPixel16; typedef guchar RrPixel8; @@ -238,24 +239,43 @@ struct _RrImagePic { /* The sum of all the pixels. This is used to compare pictures if their hashes match. */ gint sum; - /* The name of the image. This is used to determine - if the named image already is loaded. May be NULL if the image - was not loaded from disk. */ - gchar *name; }; typedef void (*RrImageDestroyFunc)(RrImage *image, gpointer data); -/*! An RrImage is a sort of meta-image. It can contain multiple versions of - an image at different sizes, which may or may not be completely different - pictures */ +/*! An RrImage refers to a RrImageSet. If multiple RrImageSets end up + holding the same image data, they will be marged and the RrImages that + point to them would be updated. */ struct _RrImage { gint ref; + RrImageSet *set; + + /* This function (if not NULL) will be called just before destroying + RrImage. */ + RrImageDestroyFunc destroy_func; + gpointer destroy_data; +}; + +/*! An RrImage is a sort of meta-image. It can contain multiple versions + of an image at different sizes, which may or may not be completely different + pictures */ +struct _RrImageSet +{ RrImageCache *cache; + /*! If a picture is loaded by a name, then it has a name attached to it. + This contains a list of strings, containing all names that have ever + been associated with the RrImageSet. A name in the RrImageCache can + only be associated with a single RrImageSet. */ + GSList *names; + + /*! RrImages that point at this RrImageSet. If this is empty, then there + are no images using the set and it can be freed. */ + GSList *images; + /*! An array of "originals", that is of RrPictures that have been added to the image in various sizes, and that have not been resized. These - are explicitly added to the RrImage. */ + are explicitly added to the RrImageSet. */ RrImagePic **original; gint n_original; /*! An array of "resized" pictures. When an "original" RrPicture @@ -264,11 +284,6 @@ struct _RrImage { RrImage. */ RrImagePic **resized; gint n_resized; - - /* This function (if not NULL) will be called just before destroying - RrImage. */ - RrImageDestroyFunc destroy_func; - gpointer destroy_data; }; /* these are the same on all endian machines because it seems to be dependant @@ -355,23 +370,41 @@ RrImageCache* RrImageCacheNew(gint max_resized_saved); void RrImageCacheRef(RrImageCache *self); void RrImageCacheUnref(RrImageCache *self); -/*! Finds an image in the cache, if it is already in there */ -RrImage* RrImageCacheFind(RrImageCache *self, - RrPixel32 *data, gint w, gint h); -/*! Finds an image in the cache, by searching for the name of the image */ -RrImage* RrImageCacheFindName(RrImageCache *self, - const gchar *name); - -RrImage* RrImageNew(RrImageCache *cache); -void RrImageRef(RrImage *im); -void RrImageUnref(RrImage *im); - -void RrImageAddPicture(RrImage *im, const RrPixel32 *data, gint w, gint h); -/*! Adds a picture by name, from a file on disk. - @name Can be a full path to an image, or it can be a name as per the - freedesktop.org icon spec. */ -gboolean RrImageAddPictureName(RrImage *im, const gchar *name); -void RrImageRemovePicture(RrImage *im, gint w, gint h); +/*! Create a new image, or return one from the cache that matches. + @param cache The image cache. + @param old The current RrImage, which the new image should be added to. + Use this if loading a different sized version of the same image. + The returned RrImage should replace the one passed in as old. + Pass NULL here if adding an image which is (or may be) entirely new. + @param name The name of the icon to be loaded off disk, or used in the cache + @return Returns NULL if unable to load an image by the name and it is not in + the cache already +*/ +RrImage* RrImageNewFromName(RrImageCache *cache, const gchar *name); + +/*! Create a new image, or return one from the cache that matches. + @param cache The image cache. + @param data The image data in RGBA32 format. There should be @w * @h many + values in the data array. + @param w The width of the image data. + @param h The height of the image data. + @return Returns NULL if unable to load an image by the name and it is not in + the cache already +*/ +RrImage* RrImageNewFromData(RrImageCache *cache, RrPixel32 *data, + gint w, gint h); + +/*! Add a new size of a picture to an image. + If a picture has multiple versions of different sizes (example 16x16, 32x32 + and so on), they should all be under the same RrImage. This adds a new + size to an existing RrImage, associating the newly sized picture with the + others in the RrImage - classifying them as being the same logical image at a + different dimention. +*/ +void RrImageAddFromData(RrImage *image, RrPixel32 *data, gint w, gint h); + +void RrImageRef(RrImage *im); +void RrImageUnref(RrImage *im); G_END_DECLS diff --git a/openbox/client.c b/openbox/client.c index 012454c..ccc64c6 100644 --- a/openbox/client.c +++ b/openbox/client.c @@ -114,18 +114,9 @@ static gboolean client_can_steal_focus(ObClient *self, void client_startup(gboolean reconfig) { - if ((client_default_icon = RrImageCacheFind(ob_rr_icons, - ob_rr_theme->def_win_icon, - ob_rr_theme->def_win_icon_w, - ob_rr_theme->def_win_icon_h))) - RrImageRef(client_default_icon); - else { - client_default_icon = RrImageNew(ob_rr_icons); - RrImageAddPicture(client_default_icon, - ob_rr_theme->def_win_icon, - ob_rr_theme->def_win_icon_w, - ob_rr_theme->def_win_icon_h); - } + client_default_icon = RrImageNewFromData( + ob_rr_icons, ob_rr_theme->def_win_icon, + ob_rr_theme->def_win_icon_w, ob_rr_theme->def_win_icon_h); if (reconfig) return; @@ -2092,7 +2083,6 @@ void client_update_icons(ObClient *self) guint num; guint32 *data; guint w, h, i, j; - guint num_seen; /* number of icons present */ RrImage *img; img = NULL; @@ -2105,13 +2095,15 @@ void client_update_icons(ObClient *self) if (OBT_PROP_GETA32(self->window, NET_WM_ICON, CARDINAL, &data, &num)) { /* figure out how many valid icons are in here */ i = 0; - num_seen = 0; while (i + 2 < num) { /* +2 is to make sure there is a w and h */ w = data[i++]; h = data[i++]; /* watch for the data being too small for the specified size, or for zero sized icons. */ - if (i + w*h > num || w == 0 || h == 0) break; + if (i + w*h > num || w == 0 || h == 0) { + i += w*h; + continue; + } /* convert it to the right bit order for ObRender */ for (j = 0; j < w*h; ++j) @@ -2121,29 +2113,13 @@ void client_update_icons(ObClient *self) (((data[i+j] >> 8) & 0xff) << RrDefaultGreenOffset) + (((data[i+j] >> 0) & 0xff) << RrDefaultBlueOffset); - /* is it in the cache? */ - img = RrImageCacheFind(ob_rr_icons, &data[i], w, h); - if (img) RrImageRef(img); /* own it */ + /* add it to the image cache as an original */ + if (!img) + img = RrImageNewFromData(ob_rr_icons, &data[i], w, h); + else + RrImageAddFromData(img, &data[i], w, h); i += w*h; - ++num_seen; - - /* don't bother looping anymore if we already found it in the cache - since we'll just use that! */ - if (img) break; - } - - /* if it's not in the cache yet, then add it to the cache now. - we have already converted it to the correct bit order above */ - if (!img && num_seen > 0) { - img = RrImageNew(ob_rr_icons); - i = 0; - for (j = 0; j < num_seen; ++j) { - w = data[i++]; - h = data[i++]; - RrImageAddPicture(img, &data[i], w, h); - i += w*h; - } } g_free(data); @@ -2167,15 +2143,10 @@ void client_update_icons(ObClient *self) if (xicon) { if (w > 0 && h > 0) { - /* is this icon in the cache yet? */ - img = RrImageCacheFind(ob_rr_icons, data, w, h); - if (img) RrImageRef(img); /* own it */ - - /* if not, then add it */ - if (!img) { - img = RrImageNew(ob_rr_icons); - RrImageAddPicture(img, data, w, h); - } + if (!img) + img = RrImageNewFromData(ob_rr_icons, data, w, h); + else + RrImageAddFromData(img, data, w, h); } g_free(data); diff --git a/openbox/menu.c b/openbox/menu.c index a4f62f6..6c346e8 100644 --- a/openbox/menu.c +++ b/openbox/menu.c @@ -294,19 +294,7 @@ static void parse_menu_item(xmlNodePtr node, gpointer data) if (config_menu_show_icons && obt_xml_attr_string(node, "icon", &icon)) { - RrImage *ic; - - ic = RrImageCacheFindName(ob_rr_icons, icon); - if (ic) - RrImageRef(ic); - else { - ic = RrImageNew(ob_rr_icons); - if (!RrImageAddPictureName(ic, icon)) { - RrImageUnref(ic); /* no need to keep it around */ - ic = NULL; - } - } - e->data.normal.icon = ic; + e->data.normal.icon = RrImageNewFromName(ob_rr_icons, icon); if (e->data.normal.icon) e->data.normal.icon_alpha = 0xff; -- 1.9.1