浏览代码

Add --sync option

Johannes Hofmann 7 年前
父节点
当前提交
5d67d3bef5
共有 4 个文件被更改,包括 29 次插入7 次删除
  1. 9
    3
      src/main.rs
  2. 8
    2
      src/map_view_gl.rs
  3. 8
    2
      src/tile_atlas.rs
  4. 4
    0
      src/tile_loader.rs

+ 9
- 3
src/main.rs 查看文件

221
                     .map_err(|e| e.description().to_string())
221
                     .map_err(|e| e.description().to_string())
222
             })
222
             })
223
             .help("Set target frames per second (default is 60). \
223
             .help("Set target frames per second (default is 60). \
224
-                  This should equal the refresh rate of the display.")
224
+                This should equal the refresh rate of the display.")
225
             .takes_value(true))
225
             .takes_value(true))
226
         .arg(Arg::with_name("offline")
226
         .arg(Arg::with_name("offline")
227
             .long("offline")
227
             .long("offline")
228
-            .help("Do not use the network"))
228
+            .help("Do not use the network. \
229
+                Try to load tiles from the offline file system cache."))
230
+        .arg(Arg::with_name("sync")
231
+            .long("sync")
232
+            .help("Load tiles in a synchronous fashion. \
233
+                Interaction is not possible while tiles are loading."))
229
         .get_matches();
234
         .get_matches();
230
 
235
 
231
     let config = if let Some(config_path) = matches.value_of_os("config") {
236
     let config = if let Some(config_path) = matches.value_of_os("config") {
253
             &cx,
258
             &cx,
254
             window.get_inner_size_pixels().unwrap(),
259
             window.get_inner_size_pixels().unwrap(),
255
             move || { proxy.wakeup_event_loop(); },
260
             move || { proxy.wakeup_event_loop(); },
256
-            !matches.is_present("offline")
261
+            !matches.is_present("offline"),
262
+            !matches.is_present("sync"),
257
         )
263
         )
258
     };
264
     };
259
 
265
 

+ 8
- 2
src/map_view_gl.rs 查看文件

23
 }
23
 }
24
 
24
 
25
 impl<'a> MapViewGl<'a> {
25
 impl<'a> MapViewGl<'a> {
26
-    pub fn new<F>(cx: &Context, initial_size: (u32, u32), update_func: F, use_network: bool) -> MapViewGl
26
+    pub fn new<F>(
27
+        cx: &Context,
28
+        initial_size: (u32, u32),
29
+        update_func: F,
30
+        use_network: bool,
31
+        use_async: bool,
32
+        ) -> MapViewGl
27
         where F: Fn() + Sync + Send + 'static,
33
         where F: Fn() + Sync + Send + 'static,
28
     {
34
     {
29
         unsafe {
35
         unsafe {
81
                 viewport_size: initial_size,
87
                 viewport_size: initial_size,
82
                 map_view: map_view,
88
                 map_view: map_view,
83
                 tile_cache: TileCache::new(move |_tile| update_func(), use_network),
89
                 tile_cache: TileCache::new(move |_tile| update_func(), use_network),
84
-                tile_atlas: TileAtlas::new(tex, 256),
90
+                tile_atlas: TileAtlas::new(tex, 256, use_async),
85
             }
91
             }
86
         }
92
         }
87
     }
93
     }

+ 8
- 2
src/tile_atlas.rs 查看文件

22
     tile_size: u32,
22
     tile_size: u32,
23
     slots_lru: LinkedHashMap<CacheSlot, Option<Tile>>, // LRU cache of slots
23
     slots_lru: LinkedHashMap<CacheSlot, Option<Tile>>, // LRU cache of slots
24
     tile_to_slot: HashMap<Tile, CacheSlot>,
24
     tile_to_slot: HashMap<Tile, CacheSlot>,
25
+    use_async: bool,
25
 }
26
 }
26
 
27
 
27
 impl<'a> TileAtlas<'a> {
28
 impl<'a> TileAtlas<'a> {
50
         self.tile_to_slot.reserve(num_slots);
51
         self.tile_to_slot.reserve(num_slots);
51
     }
52
     }
52
 
53
 
53
-    pub fn new(tex: Texture<'a>, tile_size: u32) -> Self {
54
+    pub fn new(tex: Texture<'a>, tile_size: u32, use_async: bool) -> Self {
54
         let mut atlas = TileAtlas {
55
         let mut atlas = TileAtlas {
55
             texture: tex,
56
             texture: tex,
56
             tile_size: tile_size,
57
             tile_size: tile_size,
57
             slots_lru: LinkedHashMap::new(),
58
             slots_lru: LinkedHashMap::new(),
58
             tile_to_slot: HashMap::new(),
59
             tile_to_slot: HashMap::new(),
60
+            use_async: use_async,
59
         };
61
         };
60
 
62
 
61
         atlas.init();
63
         atlas.init();
93
         let slot = match self.tile_to_slot.entry(tile) {
95
         let slot = match self.tile_to_slot.entry(tile) {
94
             Entry::Vacant(entry) => {
96
             Entry::Vacant(entry) => {
95
                 let img_option = if load {
97
                 let img_option = if load {
96
-                    cache.get_async(tile_coord, source, true)
98
+                    if self.use_async {
99
+                        cache.get_async(tile_coord, source, true)
100
+                    } else {
101
+                        cache.get_sync(tile_coord, source, true)
102
+                    }
97
                 } else {
103
                 } else {
98
                     cache.lookup(tile)
104
                     cache.lookup(tile)
99
                 };
105
                 };

+ 4
- 0
src/tile_loader.rs 查看文件

264
 
264
 
265
         match image::open(source.local_tile_path(tile)) {
265
         match image::open(source.local_tile_path(tile)) {
266
             Ok(img) => {
266
             Ok(img) => {
267
+                debug!("sync ok from path {:?}", tile);
267
                 Some(img)
268
                 Some(img)
268
             },
269
             },
269
             Err(_) => {
270
             Err(_) => {
284
                                             warn!("could not write file {}, {}", &path.display(), e.description());
285
                                             warn!("could not write file {}, {}", &path.display(), e.description());
285
                                         }
286
                                         }
286
                                     }
287
                                     }
288
+                                    debug!("sync ok from network {:?}", tile);
287
                                     return Some(img);
289
                                     return Some(img);
288
                                 }
290
                                 }
289
                             }
291
                             }
290
                         }
292
                         }
291
                     }
293
                     }
294
+                    debug!("sync fail from network {:?}", tile);
292
                     None
295
                     None
293
                 } else {
296
                 } else {
297
+                    debug!("sync fail from path {:?}", tile);
294
                     None
298
                     None
295
                 }
299
                 }
296
             },
300
             },