From ade2cc9abb8ca403a9ae5d1f9c145ab72ce94919 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Tue, 24 Jun 2014 18:24:02 -0700 Subject: [PATCH] altosuilib: Make map cache size configurable Systems with sufficient memory can get smoother map scrolling by making the cache larger. Would be nice to do this automatically? Signed-off-by: Keith Packard --- altosuilib/AltosUIConfigure.java | 26 ++++++++++++++ altosuilib/AltosUIMapCache.java | 45 ++++++++++++++++++++----- altosuilib/AltosUIMapCacheListener.java | 22 ++++++++++++ altosuilib/AltosUIMapView.java | 2 +- altosuilib/AltosUIPreferences.java | 38 +++++++++++++++++++++ altosuilib/Makefile.am | 1 + 6 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 altosuilib/AltosUIMapCacheListener.java diff --git a/altosuilib/AltosUIConfigure.java b/altosuilib/AltosUIConfigure.java index 9d54cfe5..5ab615e8 100644 --- a/altosuilib/AltosUIConfigure.java +++ b/altosuilib/AltosUIConfigure.java @@ -223,6 +223,31 @@ public class AltosUIConfigure row++; } + static final Integer map_caches[] = { 9, 25, 100 }; + + public void add_map_cache() { + pane.add(new JLabel("Map Cache Size"), constraints(0, 1)); + + final JComboBox map_cache = new JComboBox(map_caches); + + map_cache.setEditable(true); + map_cache.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + try { + int size = (Integer) (map_cache.getSelectedItem()); + + AltosUIPreferences.set_map_cache(size); + } catch (ClassCastException ce) { + map_cache.setSelectedItem(new Integer(AltosUIPreferences.map_cache())); + } + } + }); + + map_cache.setSelectedItem (new Integer(AltosUIPreferences.map_cache())); + pane.add(map_cache, constraints(1, 2, GridBagConstraints.BOTH)); + row++; + } + public void add_bluetooth() { } @@ -255,6 +280,7 @@ public class AltosUIConfigure add_font_size(); add_look_and_feel(); add_position(); + add_map_cache(); add_bluetooth(); add_frequencies(); diff --git a/altosuilib/AltosUIMapCache.java b/altosuilib/AltosUIMapCache.java index 3f1512df..9cc32e24 100644 --- a/altosuilib/AltosUIMapCache.java +++ b/altosuilib/AltosUIMapCache.java @@ -24,33 +24,36 @@ import java.awt.*; import java.io.*; import java.net.*; -public class AltosUIMapCache { +public class AltosUIMapCache implements AltosUIMapCacheListener { static final int success = 0; static final int loading = 1; static final int failed = 2; static final int bad_request = 3; static final int forbidden = 4; - static final int min_cache_size = 9; - static final int max_cache_size = 24; + int min_cache_size; /* configured minimum cache size */ + int cache_size; /* current cache size */ + int requested_cache_size; /* cache size computed by application */ private Object fetch_lock = new Object(); private Object cache_lock = new Object(); - int cache_size = min_cache_size; - AltosUIMapImage[] images = new AltosUIMapImage[cache_size]; long used; public void set_cache_size(int new_size) { + + requested_cache_size = new_size; + if (new_size < min_cache_size) new_size = min_cache_size; - if (new_size > max_cache_size) - new_size = max_cache_size; + if (new_size == cache_size) return; + System.out.printf("cache size now %d\n", new_size); + synchronized(cache_lock) { AltosUIMapImage[] new_images = new AltosUIMapImage[new_size]; @@ -91,8 +94,12 @@ public class AltosUIMapCache { try { image = new AltosUIMapImage(tile, store); image.used = used++; - if (images[oldest] != null) + if (images[oldest] != null) { + System.out.printf("drop %s\n", images[oldest].store.file.toString()); images[oldest].flush(); + } + + System.out.printf("load %s\n", store.file.toString()); images[oldest] = image; @@ -109,6 +116,28 @@ public class AltosUIMapCache { } } + public void map_cache_changed(int map_cache) { + min_cache_size = map_cache; + + set_cache_size(requested_cache_size); + } + + public void dispose() { + AltosUIPreferences.unregister_map_cache_listener(this); + + for (int i = 0; i < cache_size; i++) { + AltosUIMapImage image = images[i]; + + if (image != null) + image.flush(); + } + } + public AltosUIMapCache() { + min_cache_size = AltosUIPreferences.map_cache(); + + set_cache_size(0); + + AltosUIPreferences.register_map_cache_listener(this); } } diff --git a/altosuilib/AltosUIMapCacheListener.java b/altosuilib/AltosUIMapCacheListener.java new file mode 100644 index 00000000..680d123e --- /dev/null +++ b/altosuilib/AltosUIMapCacheListener.java @@ -0,0 +1,22 @@ +/* + * Copyright © 2014 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altosuilib_3; + +public interface AltosUIMapCacheListener { + public void map_cache_changed(int map_cache); +} diff --git a/altosuilib/AltosUIMapView.java b/altosuilib/AltosUIMapView.java index 1abd1731..34a85f52 100644 --- a/altosuilib/AltosUIMapView.java +++ b/altosuilib/AltosUIMapView.java @@ -370,7 +370,7 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo for (Point point : to_remove) tiles.remove(point); - cache.set_cache_size(((lower_right.y - upper_left.y) / px_size + 1) * ((lower_right.x - upper_left.x) / px_size + 1)); + cache.set_cache_size((getWidth() / px_size + 2) * (getHeight() / px_size + 2)); for (int y = upper_left.y; y <= lower_right.y; y += px_size) { for (int x = upper_left.x; x <= lower_right.x; x += px_size) { Point point = new Point(x, y); diff --git a/altosuilib/AltosUIPreferences.java b/altosuilib/AltosUIPreferences.java index 509faaff..ecab20d4 100644 --- a/altosuilib/AltosUIPreferences.java +++ b/altosuilib/AltosUIPreferences.java @@ -34,6 +34,9 @@ public class AltosUIPreferences extends AltosPreferences { /* Window position preference name */ final static String positionPreference = "POSITION"; + /* Maps cache size preference name */ + final static String mapCachePreference = "MAP-CACHE"; + /* UI Component to pop dialogs up */ static Component component; @@ -52,6 +55,10 @@ public class AltosUIPreferences extends AltosPreferences { public static int position = AltosUILib.position_top_left; + static LinkedList map_cache_listeners; + + public static int map_cache = 9; + public static void init() { AltosPreferences.init(new AltosUIPreferencesBackend()); @@ -68,6 +75,9 @@ public class AltosUIPreferences extends AltosPreferences { position = backend.getInt(positionPreference, AltosUILib.position_top_left); position_listeners = new LinkedList(); + + map_cache = backend.getInt(mapCachePreference, 9); + map_cache_listeners = new LinkedList(); } static { init(); } @@ -215,4 +225,32 @@ public class AltosUIPreferences extends AltosPreferences { return position; } } + + public static void register_map_cache_listener(AltosUIMapCacheListener l) { + synchronized(backend) { + map_cache_listeners.add(l); + } + } + + public static void unregister_map_cache_listener(AltosUIMapCacheListener l) { + synchronized (backend) { + map_cache_listeners.remove(l); + } + } + + public static void set_map_cache(int new_map_cache) { + synchronized(backend) { + map_cache = new_map_cache; + backend.putInt(mapCachePreference, map_cache); + flush_preferences(); + for (AltosUIMapCacheListener l: map_cache_listeners) + l.map_cache_changed(map_cache); + } + } + + public static int map_cache() { + synchronized(backend) { + return map_cache; + } + } } diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index bbee6a45..56b01ec5 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -68,6 +68,7 @@ altosuilib_JAVA = \ AltosUIMapPath.java \ AltosUIMapTile.java \ AltosUIMapCache.java \ + AltosUIMapCacheListener.java \ AltosUIMapImage.java \ AltosUIMapTransform.java \ AltosUIMapRectangle.java \ -- 2.30.2