X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fplugins%2Fsat.c;h=c96ecc43b5906f0d0df2cd4f94d9e28dc46edcd9;hb=a9f70646cc15a7113ee632e8ddd6c6ad77efb68b;hp=29a7d263a56f863e4b9c9e09ba8193ddc80530ed;hpb=f383c18c9f85a1b439c6bfb2a27d652278ed019a;p=grits diff --git a/src/plugins/sat.c b/src/plugins/sat.c index 29a7d26..c96ecc4 100644 --- a/src/plugins/sat.c +++ b/src/plugins/sat.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2010 Andy Spencer + * Copyright (C) 2009-2011 Andy Spencer * * 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 @@ -24,9 +24,7 @@ */ #include -#include #include -#include #include @@ -39,32 +37,71 @@ struct _LoadTileData { GritsPluginSat *sat; GritsTile *tile; - gchar *path; + guint8 *pixels; + gboolean alpha; + gint width; + gint height; }; static gboolean _load_tile_cb(gpointer _data) { struct _LoadTileData *data = _data; - GritsPluginSat *sat = data->sat; - GritsTile *tile = data->tile; - gchar *path = data->path; + g_debug("GritsPluginSat: _load_tile_cb start"); + + 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; +} + +static void _load_tile(GritsTile *tile, gpointer _sat) +{ + GritsPluginSat *sat = _sat; + g_debug("GritsPluginSat: _load_tile start %p", g_thread_self()); + if (sat->aborted) { + g_debug("GritsPluginSat: _load_tile - aborted"); + return; + } + + /* Download tile */ + gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL); + if (!path) return; // Canceled/error /* Load pixbuf */ - g_debug("GritsPluginSat: _load_tile_cb start"); GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL); if (!pixbuf) { g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path); g_remove(path); g_free(path); - return FALSE; + return; } g_free(path); - /* Create Texture */ - 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); + /* 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; @@ -78,34 +115,7 @@ static gboolean _load_tile_cb(gpointer _data) // memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4); //} - 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, width, height, 0, - (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, 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); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glFlush(); - - tile->data = tex; - gtk_widget_queue_draw(GTK_WIDGET(sat->viewer)); - g_object_unref(pixbuf); - return FALSE; -} - -static void _load_tile(GritsTile *tile, gpointer _sat) -{ - GritsPluginSat *sat = _sat; - g_debug("GritsPluginSat: _load_tile start %p", g_thread_self()); - struct _LoadTileData *data = g_new0(struct _LoadTileData, 1); - data->sat = sat; - data->tile = tile; - data->path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL); + /* 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()); } @@ -123,12 +133,10 @@ static void _free_tile(GritsTile *tile, gpointer _sat) g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL); } -static gpointer _update_tiles(gpointer _sat) +static void _update_tiles(gpointer _, gpointer _sat) { g_debug("GritsPluginSat: _update_tiles"); GritsPluginSat *sat = _sat; - if (!g_mutex_trylock(sat->mutex)) - return NULL; GritsPoint eye; grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev); grits_tile_update(sat->tiles, &eye, @@ -136,8 +144,6 @@ static gpointer _update_tiles(gpointer _sat) _load_tile, sat); grits_tile_gc(sat->tiles, time(NULL)-10, _free_tile, sat); - g_mutex_unlock(sat->mutex); - return NULL; } /************* @@ -146,14 +152,7 @@ static gpointer _update_tiles(gpointer _sat) static void _on_location_changed(GritsViewer *viewer, gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat) { - g_thread_create(_update_tiles, sat, FALSE, NULL); -} - -static gpointer _threaded_init(GritsPluginSat *sat) -{ - _load_tile(sat->tiles, sat); - _update_tiles(sat); - return NULL; + g_thread_pool_push(sat->threads, NULL+1, NULL); } /*********** @@ -174,7 +173,8 @@ GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer) sat->viewer = g_object_ref(viewer); /* Load initial tiles */ - g_thread_create((GThreadFunc)_threaded_init, sat, FALSE, NULL); + _load_tile(sat->tiles, sat); + _update_tiles(NULL, sat); /* Connect signals */ sat->sigid = g_signal_connect(sat->viewer, "location-changed", @@ -205,21 +205,29 @@ static void grits_plugin_sat_init(GritsPluginSat *sat) { g_debug("GritsPluginSat: init"); /* Set defaults */ - sat->mutex = g_mutex_new(); + sat->threads = g_thread_pool_new(_update_tiles, 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 grits_plugin_sat_dispose(GObject *gobject) { g_debug("GritsPluginSat: dispose"); GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject); + sat->aborted = TRUE; /* Drop references */ if (sat->viewer) { - g_signal_handler_disconnect(sat->viewer, sat->sigid); - g_object_unref(sat->viewer); + GritsViewer *viewer = sat->viewer; sat->viewer = NULL; + g_signal_handler_disconnect(viewer, sat->sigid); + grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles)); + soup_session_abort(sat->wms->http->soup); + g_thread_pool_free(sat->threads, TRUE, TRUE); + while (gtk_events_pending()) + gtk_main_iteration(); + g_object_unref(viewer); } G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject); } @@ -228,9 +236,8 @@ static void grits_plugin_sat_finalize(GObject *gobject) g_debug("GritsPluginSat: finalize"); GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject); /* Free data */ - grits_tile_free(sat->tiles, _free_tile, sat); grits_wms_free(sat->wms); - g_mutex_free(sat->mutex); + grits_tile_free(sat->tiles, _free_tile, sat); G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject); }