]> Pileus Git - grits/blobdiff - src/plugins/sat.c
Move threading out of tile update/gc functions
[grits] / src / plugins / sat.c
index 8fe6e091ab27a3de2bd2807b8ceeab12cf6e9c81..5389ddb3c82e7f22d5904fba8a8dc142047e3455 100644 (file)
@@ -24,6 +24,7 @@
  */
 
 #include <time.h>
+#include <string.h>
 #include <glib/gstdio.h>
 
 #include <grits.h>
 #define TILE_WIDTH     1024
 #define TILE_HEIGHT    512
 
-struct _LoadTileData {
-       GritsPluginSat *sat;
-       GritsTile      *tile;
-       guint8         *pixels;
-       gboolean        alpha;
-       gint            width;
-       gint            height;
-};
-static gboolean _load_tile_cb(gpointer _data)
+static void _load_tile_thread(gpointer _tile, gpointer _sat)
 {
-       struct _LoadTileData *data = _data;
-       g_debug("GritsPluginSat: _load_tile_cb start");
-       if (data->sat->aborted) {
-               g_debug("GritsPluginSat: _load_tile - aborted");
-               return FALSE;
-       }
-
-       guint *tex = g_new0(guint, 1);
-       glGenTextures(1, tex);
-       glBindTexture(GL_TEXTURE_2D, *tex);
-
-       glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-       glPixelStorei(GL_PACK_ALIGNMENT, 1);
-       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();
-
-       data->tile->data = tex;
-       gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
-       g_free(data->pixels);
-       g_free(data);
-       return FALSE;
-}
+       GritsTile      *tile = _tile;
+       GritsPluginSat *sat  = _sat;
 
-static void _load_tile(GritsTile *tile, gpointer _sat)
-{
-       GritsPluginSat *sat = _sat;
-       g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
+       g_debug("GritsPluginSat: _load_tile_thread start %p - tile=%p",
+                       g_thread_self(), tile);
        if (sat->aborted) {
-               g_debug("GritsPluginSat: _load_tile - aborted");
+               g_debug("GritsPluginSat: _load_tile_thread - aborted");
                return;
        }
 
@@ -88,66 +54,40 @@ static void _load_tile(GritsTile *tile, gpointer _sat)
        /* Load pixbuf */
        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
        if (!pixbuf) {
-               g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
+               g_warning("GritsPluginSat: _load_tile_thread - Error loading pixbuf %s", path);
                g_remove(path);
                g_free(path);
                return;
        }
        g_free(path);
 
-       /* Copy pixbuf data for callback */
-       struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
-       data->sat    = sat;
-       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);
-
        /* Draw a border */
-       //gint border = 10;
-       //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
-       //for (int i = 0; i < border; i++) {
-       //      memset(&pixels[(       i)*stride], 0xff, stride);
-       //      memset(&pixels[(height-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);
-       //}
+#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 */
-       g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
-       g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
+       grits_tile_load_pixbuf(tile, pixbuf);
+       g_debug("GritsPluginSat: _load_tile_thread end %p", g_thread_self());
 }
 
-static gboolean _free_tile_cb(gpointer data)
-{
-       glDeleteTextures(1, data);
-       g_free(data);
-       return FALSE;
-}
-static void _free_tile(GritsTile *tile, gpointer _sat)
+static void _load_tile_func(GritsTile *tile, gpointer _sat)
 {
-       g_debug("GritsPluginSat: _free_tile: %p", tile->data);
-       if (tile->data)
-               g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
-}
-
-static void _update_tiles(gpointer _, gpointer _sat)
-{
-       g_debug("GritsPluginSat: _update_tiles");
+       g_debug("GritsPluginSat: __load_tile_func - tile=%p", tile);
        GritsPluginSat *sat = _sat;
-       GritsPoint eye;
-       grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
-       grits_tile_update(sat->tiles, &eye,
-                       MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
-                       _load_tile, sat);
-       grits_tile_gc(sat->tiles, time(NULL)-10,
-                       _free_tile, sat);
+       g_thread_pool_push(sat->threads, tile, NULL);
 }
 
 /*************
@@ -156,7 +96,11 @@ static void _update_tiles(gpointer _, gpointer _sat)
 static void _on_location_changed(GritsViewer *viewer,
                gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
 {
-       g_thread_pool_push(sat->threads, NULL+1, 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);
 }
 
 /***********
@@ -177,7 +121,9 @@ GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
        sat->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
-       _update_tiles(NULL, sat);
+       gdouble lat, lon, elev;
+       grits_viewer_get_location(viewer, &lat, &lon, &elev);
+       _on_location_changed(viewer, lat, lon, elev, sat);
 
        /* Connect signals */
        sat->sigid = g_signal_connect(sat->viewer, "location-changed",
@@ -208,7 +154,7 @@ static void grits_plugin_sat_init(GritsPluginSat *sat)
 {
        g_debug("GritsPluginSat: init");
        /* Set defaults */
-       sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
+       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",
@@ -223,14 +169,12 @@ static void grits_plugin_sat_dispose(GObject *gobject)
        /* Drop references */
        if (sat->viewer) {
                GritsViewer *viewer = sat->viewer;
-               sat->viewer = NULL;
                g_signal_handler_disconnect(viewer, sat->sigid);
-               grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
-               g_object_unref(sat->tiles);
                soup_session_abort(sat->wms->http->soup);
                g_thread_pool_free(sat->threads, TRUE, TRUE);
-               while (gtk_events_pending())
-                       gtk_main_iteration();
+               sat->viewer = NULL;
+               grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
+               g_object_unref(sat->tiles);
                g_object_unref(viewer);
        }
        G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
@@ -241,7 +185,7 @@ static void grits_plugin_sat_finalize(GObject *gobject)
        GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
        /* Free data */
        grits_wms_free(sat->wms);
-       grits_tile_free(sat->tiles, _free_tile, sat);
+       grits_tile_free(sat->tiles, NULL, sat);
        G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
 
 }