altosuilib: Make map cache size configurable
authorKeith Packard <keithp@keithp.com>
Wed, 25 Jun 2014 01:24:02 +0000 (18:24 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 25 Jun 2014 01:25:18 +0000 (18:25 -0700)
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 <keithp@keithp.com>
altosuilib/AltosUIConfigure.java
altosuilib/AltosUIMapCache.java
altosuilib/AltosUIMapCacheListener.java [new file with mode: 0644]
altosuilib/AltosUIMapView.java
altosuilib/AltosUIPreferences.java
altosuilib/Makefile.am

index 9d54cfe50d2f81f698f42c4c5dc238d8e7f83d84..5ab615e8ed92b890d87e0b79a64f2f694bc0ce7d 100644 (file)
@@ -223,6 +223,31 @@ public class AltosUIConfigure
                row++;
        }
 
                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<Integer> map_cache = new JComboBox<Integer>(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() {
        }
 
        public void add_bluetooth() {
        }
 
@@ -255,6 +280,7 @@ public class AltosUIConfigure
                add_font_size();
                add_look_and_feel();
                add_position();
                add_font_size();
                add_look_and_feel();
                add_position();
+               add_map_cache();
                add_bluetooth();
                add_frequencies();
 
                add_bluetooth();
                add_frequencies();
 
index 3f1512dfdaa2eb10531cfac6bf7b915917fd1d9d..9cc32e24d4adec1544f1d69c8eba6e915c63932c 100644 (file)
@@ -24,33 +24,36 @@ import java.awt.*;
 import java.io.*;
 import java.net.*;
 
 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        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();
 
 
        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) {
        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 < 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;
 
                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];
 
                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++;
                        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();
                                        images[oldest].flush();
+                               }
+
+                               System.out.printf("load %s\n", store.file.toString());
 
                                images[oldest] = image;
 
 
                                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() {
        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 (file)
index 0000000..680d123
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright © 2014 Keith Packard <keithp@keithp.com>
+ *
+ * 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);
+}
index 1abd173121852a321ac17e40c738e0eb3c28dc54..34a85f52efb992b8ef2f4df2e6ee030ce2eb15cf 100644 (file)
@@ -370,7 +370,7 @@ public class AltosUIMapView extends Component implements MouseMotionListener, Mo
                for (Point point : to_remove)
                        tiles.remove(point);
 
                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);
                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);
index 509faaff90b0653bf32966b521990ec339538b54..ecab20d4ee7e54846660002fe4a5775dad110b5d 100644 (file)
@@ -34,6 +34,9 @@ public class AltosUIPreferences extends AltosPreferences {
        /* Window position preference name */
        final static String positionPreference = "POSITION";
 
        /* 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;
 
        /* 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;
 
 
        public static int position = AltosUILib.position_top_left;
 
+       static LinkedList<AltosUIMapCacheListener> map_cache_listeners;
+
+       public static int map_cache = 9;
+
        public static void init() {
                AltosPreferences.init(new AltosUIPreferencesBackend());
 
        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<AltosPositionListener>();
 
                position = backend.getInt(positionPreference, AltosUILib.position_top_left);
                position_listeners = new LinkedList<AltosPositionListener>();
+
+               map_cache = backend.getInt(mapCachePreference, 9);
+               map_cache_listeners = new LinkedList<AltosUIMapCacheListener>();
        }
 
        static { init(); }
        }
 
        static { init(); }
@@ -215,4 +225,32 @@ public class AltosUIPreferences extends AltosPreferences {
                        return position;
                }
        }
                        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;
+               }
+       }
 }
 }
index bbee6a45df3b9760114fd2bd1bde999c5facf71d..56b01ec53fbf182c78739a6bf888d2eec1708221 100644 (file)
@@ -68,6 +68,7 @@ altosuilib_JAVA = \
        AltosUIMapPath.java \
        AltosUIMapTile.java \
        AltosUIMapCache.java \
        AltosUIMapPath.java \
        AltosUIMapTile.java \
        AltosUIMapCache.java \
+       AltosUIMapCacheListener.java \
        AltosUIMapImage.java \
        AltosUIMapTransform.java \
        AltosUIMapRectangle.java \
        AltosUIMapImage.java \
        AltosUIMapTransform.java \
        AltosUIMapRectangle.java \