]> Pileus Git - grits/blobdiff - src/plugins/sat.c
Move threading out of tile update/gc functions
[grits] / src / plugins / sat.c
index 5a5208b8ac6380d593b71bd662999019b1e9ceb9..5389ddb3c82e7f22d5904fba8a8dc142047e3455 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:sat
+ * @short_description: Satellite plugin
+ *
+ * #GritsPluginSat provides overlays using satellite imagery. This is mostly
+ * provided by NASA's Blue Marble Next Generation.
+ */
+
 #include <time.h>
+#include <string.h>
 #include <glib/gstdio.h>
-#include <GL/gl.h>
 
-#include <gis.h>
+#include <grits.h>
 
 #include "sat.h"
 
 #define TILE_WIDTH     1024
 #define TILE_HEIGHT    512
 
-struct _LoadTileData {
-       GisPluginSat *self;
-       GisTile      *tile;
-       GdkPixbuf    *pixbuf;
-};
-static gboolean _load_tile_cb(gpointer _data)
+static void _load_tile_thread(gpointer _tile, gpointer _sat)
 {
-       struct _LoadTileData *data = _data;
-       GisPluginSat *self   = data->self;
-       GisTile      *tile   = data->tile;
-       GdkPixbuf    *pixbuf = data->pixbuf;
-       g_free(data);
-
-       /* Create Texture */
-       g_debug("GisPluginSat: _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);
-
-       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);
-       glFlush();
-       gis_viewer_end(self->viewer);
-
-       tile->data = tex;
-       gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
-       g_object_unref(pixbuf);
-       return FALSE;
-}
+       GritsTile      *tile = _tile;
+       GritsPluginSat *sat  = _sat;
+
+       g_debug("GritsPluginSat: _load_tile_thread start %p - tile=%p",
+                       g_thread_self(), tile);
+       if (sat->aborted) {
+               g_debug("GritsPluginSat: _load_tile_thread - aborted");
+               return;
+       }
 
-static void _load_tile(GisTile *tile, gpointer _self)
-{
-       GisPluginSat *self = _self;
-       g_debug("GisPluginSat: _load_tile start %p", g_thread_self());
-       char *path = gis_wms_fetch(self->wms, tile, GIS_ONCE, NULL, NULL);
-       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("GisPluginSat: _load_tile - Error loading pixbuf %s", path);
-               g_free(data);
+       /* Download tile */
+       gchar *path = grits_wms_fetch(sat->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("GritsPluginSat: _load_tile_thread - Error loading pixbuf %s", path);
                g_remove(path);
+               g_free(path);
+               return;
        }
        g_free(path);
-       g_debug("GisPluginSat: _load_tile end %p", g_thread_self());
-}
 
-static gboolean _free_tile_cb(gpointer data)
-{
-       glDeleteTextures(1, data);
-       g_free(data);
-       return FALSE;
-}
-static void _free_tile(GisTile *tile, gpointer _self)
-{
-       GisPluginSat *self = _self;
-       g_debug("GisPluginSat: _free_tile: %p=%d", tile->data, *(guint*)tile->data);
-       g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
+       /* Draw a border */
+#ifdef DRAW_TILE_BORDER
+       gint    border = 10;
+       gint    width  = gdk_pixbuf_get_width(pixbuf);
+       gint    height = gdk_pixbuf_get_height(pixbuf);
+       gint    stride = gdk_pixbuf_get_rowstride(pixbuf);
+       guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
+       for (int i = 0; i < border; i++) {
+               memset(&pixels[(         i)*stride], 0xff, stride);
+               memset(&pixels[(height-1-i)*stride], 0xff, stride);
+       }
+       for (int i = 0; i < height; i++) {
+               memset(&pixels[(i*stride)                   ], 0xff, border*4);
+               memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
+       }
+#endif
+
+       /* Load the GL texture from the main thread */
+       grits_tile_load_pixbuf(tile, pixbuf);
+       g_debug("GritsPluginSat: _load_tile_thread end %p", g_thread_self());
 }
 
-static gpointer _update_tiles(gpointer _self)
+static void _load_tile_func(GritsTile *tile, gpointer _sat)
 {
-       g_debug("GisPluginSat: _update_tiles");
-       GisPluginSat *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,
-                       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;
+       g_debug("GritsPluginSat: __load_tile_func - tile=%p", tile);
+       GritsPluginSat *sat = _sat;
+       g_thread_pool_push(sat->threads, tile, NULL);
 }
 
 /*************
  * Callbacks *
  *************/
-static void _on_location_changed(GisViewer *viewer,
-               gdouble lat, gdouble lon, gdouble elev, GisPluginSat *self)
+static void _on_location_changed(GritsViewer *viewer,
+               gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
 {
-       g_thread_create(_update_tiles, self, FALSE, NULL);
+       GritsPoint eye = {lat, lon, elev};
+       grits_tile_update(sat->tiles, &eye,
+                       MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
+                       _load_tile_func, sat);
+       grits_tile_gc(sat->tiles, time(NULL)-10, NULL, sat);
 }
 
 /***********
  * Methods *
  ***********/
-GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
+/**
+ * grits_plugin_sat_new:
+ * @viewer: the #GritsViewer to use for drawing
+ *
+ * Create a new instance of the satellite plugin.
+ *
+ * Returns: the new #GritsPluginSat
+ */
+GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
 {
-       g_debug("GisPluginSat: new");
-       GisPluginSat *self = g_object_new(GIS_TYPE_PLUGIN_SAT, NULL);
-       self->viewer = g_object_ref(viewer);
+       g_debug("GritsPluginSat: new");
+       GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
+       sat->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
-       _load_tile(self->tiles, self);
-       g_thread_create(_update_tiles, self, FALSE, NULL);
+       gdouble lat, lon, elev;
+       grits_viewer_get_location(viewer, &lat, &lon, &elev);
+       _on_location_changed(viewer, lat, lon, elev, sat);
 
        /* Connect signals */
-       self->sigid = g_signal_connect(self->viewer, "location-changed",
-                       G_CALLBACK(_on_location_changed), self);
+       sat->sigid = g_signal_connect(sat->viewer, "location-changed",
+                       G_CALLBACK(_on_location_changed), sat);
 
        /* Add renderers */
-       gis_viewer_add(viewer, GIS_OBJECT(self->tiles), GIS_LEVEL_WORLD, 0);
+       grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
 
-       return self;
+       return sat;
 }
 
 
@@ -156,53 +140,59 @@ GisPluginSat *gis_plugin_sat_new(GisViewer *viewer)
  * GObject code *
  ****************/
 /* Plugin init */
-static void gis_plugin_sat_plugin_init(GisPluginInterface *iface);
-G_DEFINE_TYPE_WITH_CODE(GisPluginSat, gis_plugin_sat, G_TYPE_OBJECT,
-               G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
-                       gis_plugin_sat_plugin_init));
-static void gis_plugin_sat_plugin_init(GisPluginInterface *iface)
+static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
+G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
+               G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
+                       grits_plugin_sat_plugin_init));
+static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
 {
-       g_debug("GisPluginSat: plugin_init");
+       g_debug("GritsPluginSat: plugin_init");
        /* Add methods to the interface */
 }
 /* Class/Object init */
-static void gis_plugin_sat_init(GisPluginSat *self)
+static void grits_plugin_sat_init(GritsPluginSat *sat)
 {
-       g_debug("GisPluginSat: init");
+       g_debug("GritsPluginSat: init");
        /* Set defaults */
-       self->mutex = g_mutex_new();
-       self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
-       self->wms   = gis_wms_new(
+       sat->threads = g_thread_pool_new(_load_tile_thread, sat, 1, FALSE, NULL);
+       sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
+       sat->wms   = grits_wms_new(
                "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
                "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
+       g_object_ref(sat->tiles);
 }
-static void gis_plugin_sat_dispose(GObject *gobject)
+static void grits_plugin_sat_dispose(GObject *gobject)
 {
-       g_debug("GisPluginSat: dispose");
-       GisPluginSat *self = GIS_PLUGIN_SAT(gobject);
+       g_debug("GritsPluginSat: dispose");
+       GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
+       sat->aborted = TRUE;
        /* Drop references */
-       if (self->viewer) {
-               g_signal_handler_disconnect(self->viewer, self->sigid);
-               g_object_unref(self->viewer);
-               self->viewer = NULL;
+       if (sat->viewer) {
+               GritsViewer *viewer = sat->viewer;
+               g_signal_handler_disconnect(viewer, sat->sigid);
+               soup_session_abort(sat->wms->http->soup);
+               g_thread_pool_free(sat->threads, TRUE, TRUE);
+               sat->viewer = NULL;
+               grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
+               g_object_unref(sat->tiles);
+               g_object_unref(viewer);
        }
-       G_OBJECT_CLASS(gis_plugin_sat_parent_class)->dispose(gobject);
+       G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
 }
-static void gis_plugin_sat_finalize(GObject *gobject)
+static void grits_plugin_sat_finalize(GObject *gobject)
 {
-       g_debug("GisPluginSat: finalize");
-       GisPluginSat *self = GIS_PLUGIN_SAT(gobject);
+       g_debug("GritsPluginSat: finalize");
+       GritsPluginSat *sat = GRITS_PLUGIN_SAT(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_sat_parent_class)->finalize(gobject);
+       grits_wms_free(sat->wms);
+       grits_tile_free(sat->tiles, NULL, sat);
+       G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
 
 }
-static void gis_plugin_sat_class_init(GisPluginSatClass *klass)
+static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
 {
-       g_debug("GisPluginSat: class_init");
+       g_debug("GritsPluginSat: class_init");
        GObjectClass *gobject_class = (GObjectClass*)klass;
-       gobject_class->dispose  = gis_plugin_sat_dispose;
-       gobject_class->finalize = gis_plugin_sat_finalize;
+       gobject_class->dispose  = grits_plugin_sat_dispose;
+       gobject_class->finalize = grits_plugin_sat_finalize;
 }