commit 37bf908cb79995f438dadd4a771369a90c34261f
parent 65ba90275359e88e967b7da1dc47d2d5a4d2715e
Author: Andrej Kacian <ticho@claws-mail.org>
Date: Fri, 8 Feb 2019 19:42:54 +0100
Implement size limit for Litehtml image cache
Diffstat:
2 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/src/plugins/litehtml_viewer/container_linux.cpp b/src/plugins/litehtml_viewer/container_linux.cpp
@@ -823,15 +823,48 @@ void container_linux::fill_ellipse( cairo_t* cr, int x, int y, int width, int he
void container_linux::clear_images()
{
-/* for(images_map::iterator i = m_images.begin(); i != m_images.end(); i++)
- {
- if(i->second)
- {
- delete i->second;
+ for(auto i = m_images.begin(); i != m_images.end(); ++i) {
+ image *img = &(*i);
+
+ if (img->second) {
+ g_object_unref(img->second);
}
}
+
m_images.clear();
-*/
+}
+
+void container_linux::clear_images(gint desired_size)
+{
+ gint size = 0;
+
+ /* First, tally up size of all the stored GdkPixbufs and
+ * deallocate those which make the total size be above
+ * the desired_size limit. We will remove their list
+ * elements later. */
+ for (auto i = m_images.rbegin(); i != m_images.rend(); ++i) {
+ image *img = &(*i);
+ gint cursize;
+
+ if (img->second == NULL)
+ continue;
+
+ cursize = gdk_pixbuf_get_byte_length(img->second);
+
+ if (size + cursize > desired_size) {
+ g_object_unref(img->second);
+ img->second = NULL;
+ } else {
+ size += cursize;
+ }
+ }
+
+ /* Remove elements whose GdkPixbuf pointers point to NULL. */
+ m_images.remove_if([&](image _img) -> bool {
+ if (_img.second == NULL)
+ return true;
+ return false;
+ });
}
const litehtml::tchar_t* container_linux::get_default_font_name() const
diff --git a/src/plugins/litehtml_viewer/container_linux.h b/src/plugins/litehtml_viewer/container_linux.h
@@ -86,6 +86,10 @@ public:
void clear_images();
+ /* Trim down images cache to less than desired_size [bytes],
+ * starting from oldest stored. */
+ void clear_images(gint desired_size);
+
protected:
virtual void draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width);
virtual void fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color);