]> Pileus Git - grits/blob - src/plugins/sat.c
More reference counting updates
[grits] / src / plugins / sat.c
1 /*
2  * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:sat
20  * @short_description: Satellite plugin
21  *
22  * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23  * provided by NASA's Blue Marble Next Generation.
24  */
25
26 #include <time.h>
27 #include <glib/gstdio.h>
28
29 #include <grits.h>
30
31 #include "sat.h"
32
33 #define MAX_RESOLUTION 500
34 #define TILE_WIDTH     1024
35 #define TILE_HEIGHT    512
36
37 struct _LoadTileData {
38         GritsPluginSat *sat;
39         GritsTile      *tile;
40         guint8         *pixels;
41         gboolean        alpha;
42         gint            width;
43         gint            height;
44 };
45 static gboolean _load_tile_cb(gpointer _data)
46 {
47         struct _LoadTileData *data = _data;
48         g_debug("GritsPluginSat: _load_tile_cb start");
49         if (data->sat->aborted) {
50                 g_debug("GritsPluginSat: _load_tile - aborted");
51                 return FALSE;
52         }
53
54         guint *tex = g_new0(guint, 1);
55         glGenTextures(1, tex);
56         glBindTexture(GL_TEXTURE_2D, *tex);
57
58         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
59         glPixelStorei(GL_PACK_ALIGNMENT, 1);
60         glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
61                         (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
62         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
63         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
65         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
66         glFlush();
67
68         data->tile->data = tex;
69         gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
70         g_free(data->pixels);
71         g_free(data);
72         return FALSE;
73 }
74
75 static void _load_tile(GritsTile *tile, gpointer _sat)
76 {
77         GritsPluginSat *sat = _sat;
78         g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
79         if (sat->aborted) {
80                 g_debug("GritsPluginSat: _load_tile - aborted");
81                 return;
82         }
83
84         /* Download tile */
85         gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
86         if (!path) return; // Canceled/error
87
88         /* Load pixbuf */
89         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
90         if (!pixbuf) {
91                 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
92                 g_remove(path);
93                 g_free(path);
94                 return;
95         }
96         g_free(path);
97
98         /* Copy pixbuf data for callback */
99         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
100         data->sat    = sat;
101         data->tile   = tile;
102         data->pixels = gdk_pixbuf_get_pixels(pixbuf);
103         data->alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
104         data->width  = gdk_pixbuf_get_width(pixbuf);
105         data->height = gdk_pixbuf_get_height(pixbuf);
106         data->pixels = g_memdup(data->pixels,
107                         data->width * data->height * (data->alpha ? 4 : 3));
108         g_object_unref(pixbuf);
109
110         /* Draw a border */
111         //gint border = 10;
112         //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
113         //for (int i = 0; i < border; i++) {
114         //      memset(&pixels[(       i)*stride], 0xff, stride);
115         //      memset(&pixels[(height-i)*stride], 0xff, stride);
116         //}
117         //for (int i = 0; i < height; i++) {
118         //      memset(&pixels[(i*stride)], 0xff, border*4);
119         //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
120         //}
121
122         /* Load the GL texture from the main thread */
123         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
124         g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
125 }
126
127 static gboolean _free_tile_cb(gpointer data)
128 {
129         glDeleteTextures(1, data);
130         g_free(data);
131         return FALSE;
132 }
133 static void _free_tile(GritsTile *tile, gpointer _sat)
134 {
135         g_debug("GritsPluginSat: _free_tile: %p", tile->data);
136         if (tile->data)
137                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
138 }
139
140 static void _update_tiles(gpointer _, gpointer _sat)
141 {
142         g_debug("GritsPluginSat: _update_tiles");
143         GritsPluginSat *sat = _sat;
144         GritsPoint eye;
145         grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
146         grits_tile_update(sat->tiles, &eye,
147                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
148                         _load_tile, sat);
149         grits_tile_gc(sat->tiles, time(NULL)-10,
150                         _free_tile, sat);
151 }
152
153 /*************
154  * Callbacks *
155  *************/
156 static void _on_location_changed(GritsViewer *viewer,
157                 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
158 {
159         g_thread_pool_push(sat->threads, NULL+1, NULL);
160 }
161
162 /***********
163  * Methods *
164  ***********/
165 /**
166  * grits_plugin_sat_new:
167  * @viewer: the #GritsViewer to use for drawing
168  *
169  * Create a new instance of the satellite plugin.
170  *
171  * Returns: the new #GritsPluginSat
172  */
173 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
174 {
175         g_debug("GritsPluginSat: new");
176         GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
177         sat->viewer = g_object_ref(viewer);
178
179         /* Load initial tiles */
180         _update_tiles(NULL, sat);
181
182         /* Connect signals */
183         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
184                         G_CALLBACK(_on_location_changed), sat);
185
186         /* Add renderers */
187         grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
188
189         return sat;
190 }
191
192
193 /****************
194  * GObject code *
195  ****************/
196 /* Plugin init */
197 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
198 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
199                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
200                         grits_plugin_sat_plugin_init));
201 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
202 {
203         g_debug("GritsPluginSat: plugin_init");
204         /* Add methods to the interface */
205 }
206 /* Class/Object init */
207 static void grits_plugin_sat_init(GritsPluginSat *sat)
208 {
209         g_debug("GritsPluginSat: init");
210         /* Set defaults */
211         sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
212         sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
213         sat->wms   = grits_wms_new(
214                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
215                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
216         g_object_ref(sat->tiles);
217 }
218 static void grits_plugin_sat_dispose(GObject *gobject)
219 {
220         g_debug("GritsPluginSat: dispose");
221         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
222         sat->aborted = TRUE;
223         /* Drop references */
224         if (sat->viewer) {
225                 GritsViewer *viewer = sat->viewer;
226                 sat->viewer = NULL;
227                 g_signal_handler_disconnect(viewer, sat->sigid);
228                 grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
229                 g_object_unref(sat->tiles);
230                 soup_session_abort(sat->wms->http->soup);
231                 g_thread_pool_free(sat->threads, TRUE, TRUE);
232                 while (gtk_events_pending())
233                         gtk_main_iteration();
234                 g_object_unref(viewer);
235         }
236         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
237 }
238 static void grits_plugin_sat_finalize(GObject *gobject)
239 {
240         g_debug("GritsPluginSat: finalize");
241         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
242         /* Free data */
243         grits_wms_free(sat->wms);
244         grits_tile_free(sat->tiles, _free_tile, sat);
245         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
246
247 }
248 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
249 {
250         g_debug("GritsPluginSat: class_init");
251         GObjectClass *gobject_class = (GObjectClass*)klass;
252         gobject_class->dispose  = grits_plugin_sat_dispose;
253         gobject_class->finalize = grits_plugin_sat_finalize;
254 }