]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
added glyph caching to X11 module to speed up redraw
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 9 Oct 2017 15:29:57 +0000 (11:29 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 9 Oct 2017 15:29:57 +0000 (11:29 -0400)
lib/x11.ml

index ebbabe21882af362b04f9e6df83a9deb4ec4e69d..949892c0cdd651a293f5b3fb23cb323164728b40 100644 (file)
@@ -52,6 +52,13 @@ type glyph = {
   yoff: int;
 }
 
+module Rune = struct
+  type t = int
+  let compare a b = (a - b)
+end
+
+module GlyphMap = Map.Make(Rune)
+
 external connect : unit -> unit
                  = "x11_connect"
 
@@ -105,8 +112,24 @@ external font_glyph : font -> int -> glyph
 external draw_glyph : int -> glyph -> (int * int) -> int
                     = "x11_draw_glyph"
 
+let glyph_cache = ref GlyphMap.empty
+
+let cache_update rune glyph =
+  glyph_cache := GlyphMap.add rune glyph !glyph_cache;
+  glyph
+
+let get_glyph (font : font) rune =
+  try
+    let glyph = GlyphMap.find rune !glyph_cache in
+    if (glyph.font != font.font) then
+      cache_update rune glyph
+    else
+      glyph
+  with Not_found ->
+    cache_update rune (font_glyph font rune)
+
 let draw_rune font color rune coord =
-  draw_glyph color (font_glyph font rune) coord
+  draw_glyph color (get_glyph font rune) coord
 
 let draw_char font color ch coord =
   draw_rune font color (Char.code ch) coord