]> Pileus Git - grits/blob - src/plugins/sat.c
Fix a variety of memory leaks
[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
50         guint *tex = g_new0(guint, 1);
51         glGenTextures(1, tex);
52         glBindTexture(GL_TEXTURE_2D, *tex);
53
54         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
55         glPixelStorei(GL_PACK_ALIGNMENT, 1);
56         glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
57                         (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
58         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
59         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
62         glFlush();
63
64         data->tile->data = tex;
65         gtk_widget_queue_draw(GTK_WIDGET(data->sat->viewer));
66         g_free(data->pixels);
67         g_free(data);
68         return FALSE;
69 }
70
71 static void _load_tile(GritsTile *tile, gpointer _sat)
72 {
73         GritsPluginSat *sat = _sat;
74         g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
75         if (sat->aborted) {
76                 g_debug("GritsPluginSat: _load_tile - aborted");
77                 return;
78         }
79
80         /* Download tile */
81         gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
82         if (!path) return; // Canceled/error
83
84         /* Load pixbuf */
85         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
86         if (!pixbuf) {
87                 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
88                 g_remove(path);
89                 g_free(path);
90                 return;
91         }
92         g_free(path);
93
94         /* Copy pixbuf data for callback */
95         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
96         data->sat    = sat;
97         data->tile   = tile;
98         data->pixels = gdk_pixbuf_get_pixels(pixbuf);
99         data->alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
100         data->width  = gdk_pixbuf_get_width(pixbuf);
101         data->height = gdk_pixbuf_get_height(pixbuf);
102         data->pixels = g_memdup(data->pixels,
103                         data->width * data->height * (data->alpha ? 4 : 3));
104         g_object_unref(pixbuf);
105
106         /* Draw a border */
107         //gint border = 10;
108         //gint stride = gdk_pixbuf_get_rowstride(pixbuf);
109         //for (int i = 0; i < border; i++) {
110         //      memset(&pixels[(       i)*stride], 0xff, stride);
111         //      memset(&pixels[(height-i)*stride], 0xff, stride);
112         //}
113         //for (int i = 0; i < height; i++) {
114         //      memset(&pixels[(i*stride)], 0xff, border*4);
115         //      memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
116         //}
117
118         /* Load the GL texture from the main thread */
119         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
120         g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
121 }
122
123 static gboolean _free_tile_cb(gpointer data)
124 {
125         glDeleteTextures(1, data);
126         g_free(data);
127         return FALSE;
128 }
129 static void _free_tile(GritsTile *tile, gpointer _sat)
130 {
131         g_debug("GritsPluginSat: _free_tile: %p", tile->data);
132         if (tile->data)
133                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
134 }
135
136 static void _update_tiles(gpointer _, gpointer _sat)
137 {
138         g_debug("GritsPluginSat: _update_tiles");
139         GritsPluginSat *sat = _sat;
140         GritsPoint eye;
141         grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
142         grits_tile_update(sat->tiles, &eye,
143                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
144                         _load_tile, sat);
145         grits_tile_gc(sat->tiles, time(NULL)-10,
146                         _free_tile, sat);
147 }
148
149 /*************
150  * Callbacks *
151  *************/
152 static void _on_location_changed(GritsViewer *viewer,
153                 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
154 {
155         g_thread_pool_push(sat->threads, NULL+1, NULL);
156 }
157
158 /***********
159  * Methods *
160  ***********/
161 /**
162  * grits_plugin_sat_new:
163  * @viewer: the #GritsViewer to use for drawing
164  *
165  * Create a new instance of the satellite plugin.
166  *
167  * Returns: the new #GritsPluginSat
168  */
169 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
170 {
171         g_debug("GritsPluginSat: new");
172         GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
173         sat->viewer = g_object_ref(viewer);
174
175         /* Load initial tiles */
176         _update_tiles(NULL, sat);
177
178         /* Connect signals */
179         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
180                         G_CALLBACK(_on_location_changed), sat);
181
182         /* Add renderers */
183         grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
184
185         return sat;
186 }
187
188
189 /****************
190  * GObject code *
191  ****************/
192 /* Plugin init */
193 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
194 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
195                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
196                         grits_plugin_sat_plugin_init));
197 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
198 {
199         g_debug("GritsPluginSat: plugin_init");
200         /* Add methods to the interface */
201 }
202 /* Class/Object init */
203 static void grits_plugin_sat_init(GritsPluginSat *sat)
204 {
205         g_debug("GritsPluginSat: init");
206         /* Set defaults */
207         sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
208         sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
209         sat->wms   = grits_wms_new(
210                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
211                 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
212         g_object_ref(sat->tiles);
213 }
214 static void grits_plugin_sat_dispose(GObject *gobject)
215 {
216         g_debug("GritsPluginSat: dispose");
217         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
218         sat->aborted = TRUE;
219         /* Drop references */
220         if (sat->viewer) {
221                 GritsViewer *viewer = sat->viewer;
222                 sat->viewer = NULL;
223                 g_signal_handler_disconnect(viewer, sat->sigid);
224                 grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
225                 soup_session_abort(sat->wms->http->soup);
226                 g_thread_pool_free(sat->threads, TRUE, TRUE);
227                 while (gtk_events_pending())
228                         gtk_main_iteration();
229                 g_object_unref(viewer);
230         }
231         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
232 }
233 static void grits_plugin_sat_finalize(GObject *gobject)
234 {
235         g_debug("GritsPluginSat: finalize");
236         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
237         /* Free data */
238         grits_wms_free(sat->wms);
239         grits_tile_free(sat->tiles, _free_tile, sat);
240         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
241
242 }
243 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
244 {
245         g_debug("GritsPluginSat: class_init");
246         GObjectClass *gobject_class = (GObjectClass*)klass;
247         gobject_class->dispose  = grits_plugin_sat_dispose;
248         gobject_class->finalize = grits_plugin_sat_finalize;
249 }