summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2016-06-02 21:00:37 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2016-06-02 21:00:37 +0200
commitab7ab9e52ab87963831f90ab25957d883878654d (patch)
tree2225a4382497889f43477429d2f6b696ce04bf8f
parent1d6833cfcb0b5bf4890fa15b5013d7490af48f69 (diff)
New ImageCache class for reusing Image resources.
-rw-r--r--plugin/Makefile.mingw32.in1
-rw-r--r--plugingui/Makefile.mingw321
-rw-r--r--plugingui/image.cc32
-rw-r--r--plugingui/image.h11
-rw-r--r--plugingui/imagecache.cc104
-rw-r--r--plugingui/imagecache.h72
-rw-r--r--test/Makefile.am16
-rw-r--r--test/imagecachetest.cc98
8 files changed, 330 insertions, 5 deletions
diff --git a/plugin/Makefile.mingw32.in b/plugin/Makefile.mingw32.in
index 16095a8..73a4866 100644
--- a/plugin/Makefile.mingw32.in
+++ b/plugin/Makefile.mingw32.in
@@ -72,6 +72,7 @@ GUI_SRC = \
@top_srcdir@/plugingui/filebrowser.cc \
@top_srcdir@/plugingui/directory.cc \
@top_srcdir@/plugingui/image.cc \
+ @top_srcdir@/plugingui/imagecache.cc \
@top_srcdir@/plugingui/combobox.cc \
@top_srcdir@/plugingui/progressbar.cc \
@top_srcdir@/plugingui/verticalline.cc \
diff --git a/plugingui/Makefile.mingw32 b/plugingui/Makefile.mingw32
index 9820d1a..98d8f29 100644
--- a/plugingui/Makefile.mingw32
+++ b/plugingui/Makefile.mingw32
@@ -35,6 +35,7 @@ GUI_SRC = \
filebrowser.cc \
directory.cc \
image.cc \
+ imagecache.cc \
combobox.cc \
progressbar.cc \
verticalline.cc \
diff --git a/plugingui/image.cc b/plugingui/image.cc
index 250dc60..63f708d 100644
--- a/plugingui/image.cc
+++ b/plugingui/image.cc
@@ -48,9 +48,39 @@ Image::Image(const std::string& filename)
load(rc.data(), rc.size());
}
+Image::Image(Image&& other)
+ : _width(other._width)
+ , _height(other._height)
+ , image_data(other.image_data)
+{
+ other.image_data = nullptr;
+ other._width = 0;
+ other._height = 0;
+}
+
Image::~Image()
{
- std::free(image_data);
+ if(image_data)
+ {
+ std::free(image_data);
+ }
+}
+
+Image& Image::operator=(Image&& other)
+{
+ if(image_data)
+ {
+ std::free(image_data);
+ }
+ image_data = other.image_data;
+ _width = other._width;
+ _height = other._height;
+
+ other.image_data = nullptr;
+ other._width = 0;
+ other._height = 0;
+
+ return *this;
}
void Image::setError()
diff --git a/plugingui/image.h b/plugingui/image.h
index 92bd181..8d1fd59 100644
--- a/plugingui/image.h
+++ b/plugingui/image.h
@@ -33,12 +33,16 @@
namespace GUI {
-class Image {
+class Image
+{
public:
Image(const char* data, size_t size);
Image(const std::string& filename);
+ Image(Image&& other);
~Image();
+ Image& operator=(Image&& other);
+
size_t width() const;
size_t height() const;
@@ -49,8 +53,9 @@ private:
void load(const char* data, size_t size);
- size_t _width, _height;
- unsigned char* image_data;
+ std::size_t _width{0};
+ std::size_t _height{0};
+ unsigned char* image_data{nullptr};
};
} // GUI::
diff --git a/plugingui/imagecache.cc b/plugingui/imagecache.cc
new file mode 100644
index 0000000..6ce1171
--- /dev/null
+++ b/plugingui/imagecache.cc
@@ -0,0 +1,104 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * imagecache.cc
+ *
+ * Thu Jun 2 17:12:05 CEST 2016
+ * Copyright 2016 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of DrumGizmo.
+ *
+ * DrumGizmo is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * DrumGizmo is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with DrumGizmo; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "imagecache.h"
+
+#include <cassert>
+
+#include "image.h"
+
+namespace GUI
+{
+
+ScopedImageBorrower::ScopedImageBorrower(ImageCache& imageCache,
+ const std::string& filename)
+ : imageCache(imageCache)
+ , filename(filename)
+ , image(imageCache.borrow(filename))
+{
+}
+
+ScopedImageBorrower::ScopedImageBorrower(ScopedImageBorrower&& other)
+ : imageCache(other.imageCache)
+ , filename(other.filename)
+ , image(other.image)
+{
+ other.filename.clear();
+}
+
+ScopedImageBorrower::~ScopedImageBorrower()
+{
+ if(!filename.empty())
+ {
+ imageCache.giveBack(filename);
+ }
+}
+
+Image& ScopedImageBorrower::operator*()
+{
+ return image;
+}
+
+Image& ScopedImageBorrower::operator()()
+{
+ return image;
+}
+
+ScopedImageBorrower ImageCache::getImage(const std::string& filename)
+{
+ return ScopedImageBorrower(*this, filename);
+}
+
+Image& ImageCache::borrow(const std::string& filename)
+{
+ auto cacheIterator = imageCache.find(filename);
+ if(cacheIterator == imageCache.end())
+ {
+ Image image(filename);
+ auto insertValue =
+ imageCache.emplace(filename,
+ std::move(std::make_pair(0, std::move(image))));
+ cacheIterator = insertValue.first;
+ }
+
+ auto& cacheEntry = cacheIterator->second;
+ ++cacheEntry.first;
+ return cacheEntry.second;
+}
+
+void ImageCache::giveBack(const std::string& filename)
+{
+ auto cacheIterator = imageCache.find(filename);
+ assert(cacheIterator != imageCache.end());
+ auto& cacheEntry = cacheIterator->second;
+ --cacheEntry.first;
+ if(cacheEntry.first == 0)
+ {
+ imageCache.erase(cacheIterator);
+ }
+}
+
+} // GUI::
diff --git a/plugingui/imagecache.h b/plugingui/imagecache.h
new file mode 100644
index 0000000..9ca28fb
--- /dev/null
+++ b/plugingui/imagecache.h
@@ -0,0 +1,72 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * imagecache.h
+ *
+ * Thu Jun 2 17:12:05 CEST 2016
+ * Copyright 2016 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of DrumGizmo.
+ *
+ * DrumGizmo is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * DrumGizmo is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with DrumGizmo; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#pragma once
+
+#include <string>
+#include <map>
+#include <utility>
+
+namespace GUI
+{
+
+class Image;
+class ImageCache;
+
+class ScopedImageBorrower
+{
+public:
+ ScopedImageBorrower(ImageCache& imageCache, const std::string& filename);
+ ScopedImageBorrower(ScopedImageBorrower&& other);
+ ~ScopedImageBorrower();
+
+ ScopedImageBorrower& operator=(ScopedImageBorrower&& other);
+
+ Image& operator*();
+ Image& operator()();
+
+private:
+ ImageCache& imageCache;
+ std::string filename;
+ Image& image;
+};
+
+class ImageCache
+{
+public:
+ ScopedImageBorrower getImage(const std::string& filename);
+
+private:
+ friend class ScopedImageBorrower;
+
+ Image& borrow(const std::string& filename);
+ void giveBack(const std::string& filename);
+
+protected:
+ std::map<std::string, std::pair<std::size_t, Image>> imageCache;
+};
+
+} // GUI::
diff --git a/test/Makefile.am b/test/Makefile.am
index 594aa0d..1b4470d 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -5,7 +5,7 @@ include $(top_srcdir)/src/Makefile.am.drumgizmo
TESTS = resource engine gui resampler lv2 configfile audiocache \
audiocachefile audiocacheidmanager audiocacheeventhandler \
- memchecker randomtest atomictest syncedsettingstest
+ memchecker randomtest atomictest syncedsettingstest imagecachetest
check_PROGRAMS = $(TESTS)
@@ -150,3 +150,17 @@ syncedsettingstest_SOURCES = syncedsettings.cc test.cc
EXTRA_DIST = \
lv2_test_host.h
+
+imagecachetest_CXXFLAGS = -DOUTPUT=\"imagecachetest\" $(CPPUNIT_CFLAGS) \
+ -I$(top_srcdir)/src -I$(top_srcdir)/plugingui -I$(top_srcdir)/hugin
+imagecachetest_LDFLAGS = $(CPPUNIT_LIBS)
+imagecachetest_SOURCES = \
+ $(top_srcdir)/hugin/hugin.c \
+ $(top_srcdir)/plugingui/lodepng/lodepng.cpp \
+ $(top_srcdir)/plugingui/resource_data.cc \
+ $(top_srcdir)/plugingui/resource.cc \
+ $(top_srcdir)/plugingui/image.cc \
+ $(top_srcdir)/plugingui/imagecache.cc \
+ $(top_srcdir)/plugingui/colour.cc \
+ imagecachetest.cc \
+ test.cc
diff --git a/test/imagecachetest.cc b/test/imagecachetest.cc
new file mode 100644
index 0000000..2a5b6ed
--- /dev/null
+++ b/test/imagecachetest.cc
@@ -0,0 +1,98 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * imagecachetest.cc
+ *
+ * Thu Jun 2 18:54:31 CEST 2016
+ * Copyright 2016 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of DrumGizmo.
+ *
+ * DrumGizmo is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * DrumGizmo is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with DrumGizmo; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <cassert>
+
+#include <imagecache.h>
+#include <image.h>
+
+class TestableImageCache
+ : public GUI::ImageCache
+{
+public:
+ std::size_t count(const std::string& filename)
+ {
+ if(imageCache.find(filename) == imageCache.end())
+ {
+ return 0;
+ }
+
+ auto it = imageCache.find(filename);
+ auto& val = it->second;
+ return val.first;
+ }
+};
+
+class ImageCacheTest
+ : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(ImageCacheTest);
+ CPPUNIT_TEST(refCountTest);
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ void setUp() {}
+ void tearDown() {}
+
+ void refCountTest()
+ {
+ TestableImageCache imageCache;
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 0u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 0u);
+
+ {
+ auto image1{imageCache.getImage("foo")};
+ (void)image1;
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 1u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 0u);
+
+ auto image2 = imageCache.getImage("bar");
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 1u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u);
+
+ auto image3 = imageCache.getImage("foo");
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 2u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u);
+
+ {
+ auto image4 = imageCache.getImage("foo");
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 3u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u);
+ }
+
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 2u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 1u);
+ }
+
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("foo"), 0u);
+ CPPUNIT_ASSERT_EQUAL(imageCache.count("bar"), 0u);
+ }
+};
+
+// Registers the fixture into the 'registry'
+CPPUNIT_TEST_SUITE_REGISTRATION(ImageCacheTest);