]> Pileus Git - grits/blobdiff - src/plugins/map.c
More reference counting updates
[grits] / src / plugins / map.c
index e30975f0df673348a89dba977324fcad7a0d18fb..13f986c10e5dd2dbdc22245ff03d0a09483c1408 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.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
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/**
+ * SECTION:map
+ * @short_description: Map plugin
+ *
+ * #GritsPluginMap provides map overlays. Much of this data is obtained from the
+ * OpenStreetMap project.
+ */
+
 #include <time.h>
 #include <glib/gstdio.h>
-#include <GL/gl.h>
 
-#include <gis.h>
+#include <grits.h>
 
 #include "map.h"
 
-#define MAX_RESOLUTION 500
-#define TILE_WIDTH     1024
-#define TILE_HEIGHT    512
+#define MAX_RESOLUTION 1
+#define TILE_WIDTH     256
+#define TILE_HEIGHT    256
+//#define MAX_RESOLUTION 100
+//#define TILE_WIDTH     1024
+//#define TILE_HEIGHT    512
 
-const guchar colormap[][2][4] = {
-       {{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x20}}, // Oceans
-       {{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
-       {{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
-       {{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
+static const guchar colormap[][2][4] = {
+       //{{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x00}}, // Oceans
+       //{{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
+       //{{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
+       //{{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
+       //{{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
 };
 
 struct _LoadTileData {
-       GisPluginMap *self;
-       GisTile      *tile;
-       GdkPixbuf    *pixbuf;
+       GritsPluginMap *map;
+       GritsTile      *tile;
+       guint8         *pixels;
+       gboolean        alpha;
+       gint            width;
+       gint            height;
 };
-#include <stdlib.h>
 static gboolean _load_tile_cb(gpointer _data)
 {
        struct _LoadTileData *data = _data;
-       GisPluginMap *self   = data->self;
-       GisTile      *tile   = data->tile;
-       GdkPixbuf    *pixbuf = data->pixbuf;
-       g_free(data);
-
-       /* Create Texture */
-       g_debug("GisPluginMap: _load_tile_cb start");
-       guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
-       gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
-       gint      width  = gdk_pixbuf_get_width(pixbuf);
-       gint      height = gdk_pixbuf_get_height(pixbuf);
-
-       for (int i = 0; i < width*height; i++) {
-               for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
-                       if (pixels[i*4+0] == colormap[j][0][0] &&
-                           pixels[i*4+1] == colormap[j][0][1] &&
-                           pixels[i*4+2] == colormap[j][0][2]) {
-                               pixels[i*4+0] = colormap[j][1][0];
-                               pixels[i*4+1] = colormap[j][1][1];
-                               pixels[i*4+2] = colormap[j][1][2];
-                               pixels[i*4+3] = colormap[j][1][3];
-                               break;
-                       }
-               }
+       g_debug("GritsPluginMap: _load_tile_cb start");
+       if (data->map->aborted) {
+               g_debug("GritsPluginMap: _load_tile - aborted");
+               return FALSE;
        }
 
        guint *tex = g_new0(guint, 1);
-       gis_viewer_begin(self->viewer);
        glGenTextures(1, tex);
        glBindTexture(GL_TEXTURE_2D, *tex);
 
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
-       glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
-                       (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
-       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
-       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+       glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
+                       (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glFlush();
-       gis_viewer_end(self->viewer);
 
-       tile->data = tex;
-       gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
-       g_object_unref(pixbuf);
+       data->tile->data = tex;
+       if (data->map->viewer)
+               gtk_widget_queue_draw(GTK_WIDGET(data->map->viewer));
+       g_free(data->pixels);
+       g_free(data);
        return FALSE;
 }
 
-static void _load_tile(GisTile *tile, gpointer _self)
+static void _load_tile(GritsTile *tile, gpointer _map)
 {
-       GisPluginMap *self = _self;
-       g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
-       char *path = gis_wms_make_local(self->wms, tile);
-       struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
-       data->self   = self;
-       data->tile   = tile;
-       data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
-       if (data->pixbuf) {
-               g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
-       } else {
-               g_warning("GisPluginMap: _load_tile - Error loading pixbuf %s", path);
+       GritsPluginMap *map = _map;
+       g_debug("GritsPluginMap: _load_tile start %p", g_thread_self());
+       if (map->aborted) {
+               g_debug("GritsPluginMap: _load_tile - aborted");
+               return;
+       }
+
+       /* Download tile */
+       gchar *path = grits_tms_fetch(map->tms, tile, GRITS_ONCE, NULL, NULL);
+       //gchar *path = grits_wms_fetch(map->wms, tile, GRITS_ONCE, NULL, NULL);
+       if (!path) return; // Canceled/error
+
+       /* Load pixbuf */
+       GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
+       if (!pixbuf) {
+               g_warning("GritsPluginMap: _load_tile - Error loading pixbuf %s", path);
                g_remove(path);
+               g_free(path);
+               return;
        }
        g_free(path);
-       g_debug("GisPluginMap: _load_tile end %p", g_thread_self());
+
+       /* Copy pixbuf data for callback */
+       struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
+       data->map    = map;
+       data->tile   = tile;
+       data->pixels = gdk_pixbuf_get_pixels(pixbuf);
+       data->alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
+       data->width  = gdk_pixbuf_get_width(pixbuf);
+       data->height = gdk_pixbuf_get_height(pixbuf);
+       data->pixels = g_memdup(data->pixels,
+                       data->width * data->height * (data->alpha ? 4 : 3));
+       g_object_unref(pixbuf);
+
+       /* Map texture colors, if needed */
+       for (int i = 0; i < data->width * data->height; i++) {
+               for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
+                       if (data->pixels[i*4+0] == colormap[j][0][0] &&
+                           data->pixels[i*4+1] == colormap[j][0][1] &&
+                           data->pixels[i*4+2] == colormap[j][0][2]) {
+                               data->pixels[i*4+0] = colormap[j][1][0];
+                               data->pixels[i*4+1] = colormap[j][1][1];
+                               data->pixels[i*4+2] = colormap[j][1][2];
+                               data->pixels[i*4+3] = colormap[j][1][3];
+                               break;
+                       }
+               }
+       }
+
+       /* Load the GL texture from the main thread */
+       g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
+       g_debug("GritsPluginMap: _load_tile end %p", g_thread_self());
 }
 
 static gboolean _free_tile_cb(gpointer data)
@@ -116,70 +146,64 @@ static gboolean _free_tile_cb(gpointer data)
        g_free(data);
        return FALSE;
 }
-static void _free_tile(GisTile *tile, gpointer _self)
+static void _free_tile(GritsTile *tile, gpointer _map)
 {
-       GisPluginMap *self = _self;
-       g_debug("GisPluginMap: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
-       g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
+       if (tile->data) {
+               g_debug("GritsPluginMap: _free_tile: %p", tile->data);
+               g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
+       }
 }
 
-static gpointer _update_tiles(gpointer _self)
+static void _update_tiles(gpointer _, gpointer _map)
 {
-       g_debug("GisPluginMap: _update_tiles");
-       GisPluginMap *self = _self;
-       g_mutex_lock(self->mutex);
-       gdouble lat, lon, elev;
-       gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
-       gis_tile_update(self->tiles,
+       g_debug("GritsPluginMap: _update_tiles");
+       GritsPluginMap *map = _map;
+       GritsPoint eye;
+       grits_viewer_get_location(map->viewer, &eye.lat, &eye.lon, &eye.elev);
+       grits_tile_update(map->tiles, &eye,
                        MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
-                       lat, lon, elev,
-                       _load_tile, self);
-       gis_tile_gc(self->tiles, time(NULL)-10,
-                       _free_tile, self);
-       g_mutex_unlock(self->mutex);
-       return NULL;
+                       _load_tile, map);
+       grits_tile_gc(map->tiles, time(NULL)-10,
+                       _free_tile, map);
 }
 
 /*************
  * Callbacks *
  *************/
-static void _on_location_changed(GisViewer *viewer,
-               gdouble lat, gdouble lon, gdouble elev, GisPluginMap *self)
-{
-       g_thread_create(_update_tiles, self, FALSE, NULL);
-}
-
-static gpointer _expose(GisCallback *callback, gpointer _self)
+static void _on_location_changed(GritsViewer *viewer,
+               gdouble lat, gdouble lon, gdouble elev, GritsPluginMap *map)
 {
-       GisPluginMap *self = GIS_PLUGIN_MAP(_self);
-       g_debug("GisPluginMap: expose viewer=%p tiles=%p,%p",
-                       self->viewer, self->tiles, self->tiles->data);
-       gis_viewer_render_tiles(self->viewer, self->tiles);
-       return NULL;
+       g_thread_pool_push(map->threads, NULL+1, NULL);
 }
 
 /***********
  * Methods *
  ***********/
-GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
+/**
+ * grits_plugin_map_new:
+ * @viewer: the #GritsViewer to use for drawing
+ *
+ * Create a new instance of the map plugin.
+ *
+ * Returns: the new #GritsPluginMap
+ */
+GritsPluginMap *grits_plugin_map_new(GritsViewer *viewer)
 {
-       g_debug("GisPluginMap: new");
-       GisPluginMap *self = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL);
-       self->viewer = g_object_ref(viewer);
+       g_debug("GritsPluginMap: new");
+       GritsPluginMap *map = g_object_new(GRITS_TYPE_PLUGIN_MAP, NULL);
+       map->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
-       _load_tile(self->tiles, self);
-       g_thread_create(_update_tiles, self, FALSE, NULL);
+       _update_tiles(NULL, map);
 
        /* Connect signals */
-       self->sigid = g_signal_connect(self->viewer, "location-changed",
-                       G_CALLBACK(_on_location_changed), self);
+       map->sigid = g_signal_connect(map->viewer, "location-changed",
+                       G_CALLBACK(_on_location_changed), map);
 
        /* Add renderers */
-       GisCallback *callback = gis_callback_new(_expose, self);
-       gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_OVERLAY, 0);
+       grits_viewer_add(viewer, GRITS_OBJECT(map->tiles), GRITS_LEVEL_WORLD, FALSE);
 
-       return self;
+       return map;
 }
 
 
@@ -187,53 +211,68 @@ GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
  * GObject code *
  ****************/
 /* Plugin init */
-static void gis_plugin_map_plugin_init(GisPluginInterface *iface);
-G_DEFINE_TYPE_WITH_CODE(GisPluginMap, gis_plugin_map, G_TYPE_OBJECT,
-               G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
-                       gis_plugin_map_plugin_init));
-static void gis_plugin_map_plugin_init(GisPluginInterface *iface)
+static void grits_plugin_map_plugin_init(GritsPluginInterface *iface);
+G_DEFINE_TYPE_WITH_CODE(GritsPluginMap, grits_plugin_map, G_TYPE_OBJECT,
+               G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
+                       grits_plugin_map_plugin_init));
+static void grits_plugin_map_plugin_init(GritsPluginInterface *iface)
 {
-       g_debug("GisPluginMap: plugin_init");
+       g_debug("GritsPluginMap: plugin_init");
        /* Add methods to the interface */
 }
 /* Class/Object init */
-static void gis_plugin_map_init(GisPluginMap *self)
+static void grits_plugin_map_init(GritsPluginMap *map)
 {
-       g_debug("GisPluginMap: init");
+       g_debug("GritsPluginMap: init");
        /* Set defaults */
-       self->mutex  = g_mutex_new();
-       self->tiles  = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
-       self->wms    = gis_wms_new(
-               "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
-               "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
+       map->threads = g_thread_pool_new(_update_tiles, map, 1, FALSE, NULL);
+       map->tiles = grits_tile_new(NULL, 85.0511, -85.0511, EAST, WEST);
+       map->tms   = grits_tms_new("http://tile.openstreetmap.org",
+               "osmtile/", "png");
+       map->tiles->proj = GRITS_PROJ_MERCATOR;
+       //map->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
+       //map->wms   = grits_wms_new(
+       //      "http://vmap0.tiles.osgeo.org/wms/vmap0",
+       //      "basic,priroad,secroad,depthcontour,clabel,statelabel",
+       //       "image/png", "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
+       g_object_ref(map->tiles);
 }
-static void gis_plugin_map_dispose(GObject *gobject)
+static void grits_plugin_map_dispose(GObject *gobject)
 {
-       g_debug("GisPluginMap: dispose");
-       GisPluginMap *self = GIS_PLUGIN_MAP(gobject);
+       g_debug("GritsPluginMap: dispose");
+       GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
+       map->aborted = TRUE;
        /* Drop references */
-       if (self->viewer) {
-               g_signal_handler_disconnect(self->viewer, self->sigid);
-               g_object_unref(self->viewer);
-               self->viewer = NULL;
+       if (map->viewer) {
+               GritsViewer *viewer = map->viewer;
+               map->viewer = NULL;
+               g_signal_handler_disconnect(viewer, map->sigid);
+               grits_viewer_remove(viewer, GRITS_OBJECT(map->tiles));
+               g_object_unref(map->tiles);
+               soup_session_abort(map->tms->http->soup);
+               //soup_session_abort(map->wms->http->soup);
+               g_thread_pool_free(map->threads, TRUE, TRUE);
+               while (gtk_events_pending())
+                       gtk_main_iteration();
+               g_object_unref(viewer);
        }
-       G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject);
+       G_OBJECT_CLASS(grits_plugin_map_parent_class)->dispose(gobject);
 }
-static void gis_plugin_map_finalize(GObject *gobject)
+static void grits_plugin_map_finalize(GObject *gobject)
 {
-       g_debug("GisPluginMap: finalize");
-       GisPluginMap *self = GIS_PLUGIN_MAP(gobject);
+       g_debug("GritsPluginMap: finalize");
+       GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
        /* Free data */
-       gis_tile_free(self->tiles, _free_tile, self);
-       gis_wms_free(self->wms);
-       g_mutex_free(self->mutex);
-       G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject);
+       grits_tms_free(map->tms);
+       //grits_wms_free(map->wms);
+       grits_tile_free(map->tiles, _free_tile, map);
+       G_OBJECT_CLASS(grits_plugin_map_parent_class)->finalize(gobject);
 
 }
-static void gis_plugin_map_class_init(GisPluginMapClass *klass)
+static void grits_plugin_map_class_init(GritsPluginMapClass *klass)
 {
-       g_debug("GisPluginMap: class_init");
+       g_debug("GritsPluginMap: class_init");
        GObjectClass *gobject_class = (GObjectClass*)klass;
-       gobject_class->dispose  = gis_plugin_map_dispose;
-       gobject_class->finalize = gis_plugin_map_finalize;
+       gobject_class->dispose  = grits_plugin_map_dispose;
+       gobject_class->finalize = grits_plugin_map_finalize;
 }