[Commits] [SCM] claws branch, litehtml, updated. 3.17.3-111-g8f77d11

ticho at claws-mail.org ticho at claws-mail.org
Fri Feb 8 19:45:18 CET 2019


The branch, litehtml has been updated
       via  8f77d1194e4d3226833c943e9a6d50080bf8111b (commit)
       via  5b433020c024567a67ec0c3fd32e21feac43db20 (commit)
      from  cefa3f92c2ba0fbbecc9ac00977a8ec918f5bc17 (commit)

Summary of changes:
 src/plugins/litehtml_viewer/container_linux.cpp |   89 ++++++++++++++++++++---
 src/plugins/litehtml_viewer/container_linux.h   |    8 +-
 2 files changed, 84 insertions(+), 13 deletions(-)


- Log -----------------------------------------------------------------
commit 8f77d1194e4d3226833c943e9a6d50080bf8111b
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Fri Feb 8 19:42:54 2019 +0100

    Implement size limit for Litehtml image cache

diff --git a/src/plugins/litehtml_viewer/container_linux.cpp b/src/plugins/litehtml_viewer/container_linux.cpp
index f24add4..e4d3c1d 100644
--- 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
index 8c6eb6d..8298437 100644
--- 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);

commit 5b433020c024567a67ec0c3fd32e21feac43db20
Author: Andrej Kacian <ticho at claws-mail.org>
Date:   Fri Feb 8 18:33:00 2019 +0100

    Switch Litehtml's image cache from std::map to std::list
    
    This makes the cache ordered, so we are able to remove
    oldest entries if we want to trim memory usage.

diff --git a/src/plugins/litehtml_viewer/container_linux.cpp b/src/plugins/litehtml_viewer/container_linux.cpp
index 26845fb..f24add4 100644
--- a/src/plugins/litehtml_viewer/container_linux.cpp
+++ b/src/plugins/litehtml_viewer/container_linux.cpp
@@ -278,14 +278,25 @@ void container_linux::load_image( const litehtml::tchar_t* src, const litehtml::
 {
 	litehtml::tstring url;
 	make_url(src, baseurl, url);
-	if(m_images.find(url.c_str()) == m_images.end())
+	bool found = false;
+
+	for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
+		const image *i = &(*ii);
+
+		if (!strcmp(i->first.c_str(), url.c_str())) {
+			found = true;
+			break;
+		}
+	}
+
+	if(!found)
 	{
 		try
 		{
 			GdkPixbuf *img = get_image(url.c_str(), true);
 			if(img)
 			{
-				m_images[url.c_str()] = img;
+				m_images.push_back(std::make_pair(url, img));
 			}
 		} catch(...)
 		{
@@ -299,9 +310,19 @@ void container_linux::get_image_size( const litehtml::tchar_t* src, const liteht
 {
 	litehtml::tstring url;
 	make_url(src, baseurl, url);
+	bool found = false;
+	const image *img = NULL;
 
-	images_map::iterator img = m_images.find(url.c_str());
-	if(img != m_images.end())
+	for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
+		const image *i = &(*ii);
+		if (i->first == url) {
+			img = i;
+			found = true;
+			break;
+		}
+	}
+
+	if(img != NULL)
 	{
 		sz.width	= gdk_pixbuf_get_width(img->second);
 		sz.height	= gdk_pixbuf_get_height(img->second);
@@ -334,8 +355,19 @@ void container_linux::draw_background( litehtml::uint_ptr hdc, const litehtml::b
 	make_url(bg.image.c_str(), bg.baseurl.c_str(), url);
 
 	//lock_images_cache();
-	images_map::iterator img_i = m_images.find(url.c_str());
-	if(img_i != m_images.end() && img_i->second)
+	bool found = false;
+	const image *img_i = NULL;
+
+	for (auto ii = m_images.cbegin(); ii != m_images.cend(); ++ii) {
+		const image *i = &(*ii);
+		if (i->first == url) {
+			img_i = i;
+			found = true;
+			break;
+		}
+	}
+
+	if(img_i != NULL && img_i->second)
 	{
 		GdkPixbuf *bgbmp = img_i->second;
 
diff --git a/src/plugins/litehtml_viewer/container_linux.h b/src/plugins/litehtml_viewer/container_linux.h
index 94eafab..8c6eb6d 100644
--- a/src/plugins/litehtml_viewer/container_linux.h
+++ b/src/plugins/litehtml_viewer/container_linux.h
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <vector>
+#include <list>
 #include <string>
 
 #include <cairo.h>
@@ -44,7 +45,8 @@ struct cairo_font
 
 class container_linux :	public litehtml::document_container
 {
-	typedef std::map<litehtml::tstring, GdkPixbuf* >	images_map;
+	typedef std::pair<litehtml::tstring, GdkPixbuf*> image;
+	typedef std::list<image> images_map;
 
 protected:
 	cairo_surface_t*			m_temp_surface;

-----------------------------------------------------------------------


hooks/post-receive
-- 
Claws Mail


More information about the Commits mailing list