From: Michael D. Lowis Date: Mon, 9 Oct 2017 15:29:57 +0000 (-0400) Subject: added glyph caching to X11 module to speed up redraw X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=6eae54548591c2e37162eac00c2bb4d6da6d0bc3;p=archive%2Ftide-ocaml.git added glyph caching to X11 module to speed up redraw --- diff --git a/lib/x11.ml b/lib/x11.ml index ebbabe2..949892c 100644 --- a/lib/x11.ml +++ b/lib/x11.ml @@ -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