From 6eae54548591c2e37162eac00c2bb4d6da6d0bc3 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 9 Oct 2017 11:29:57 -0400 Subject: [PATCH] added glyph caching to X11 module to speed up redraw --- lib/x11.ml | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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 -- 2.52.0