[Haskell-cafe] Diagrams and GTK

Claude Heiland-Allen claude at mathr.co.uk
Fri Aug 9 17:39:18 CEST 2013

Hi Michael,

On 09/08/13 08:21, Michael Oswald wrote:
> Hello,
> I am currently writing an application which draws the structure of some
> packets with help of the diagrams library directly to a GTK GUI.
> Now the packets can have several hundreds of parameters which have to be
> drawn so it takes some seconds to calculate the diagram itself. Of
> course this now blocks the GUI thread, so the basic idea was to put the
> calculation in a separate thread. Unfortunately this doesn't work as
> lazyness kicks in and the final diagram is calculated when it is
> rendered and not evaluated before. Using seq didn't help (because of
> WHNF) and there seems to be no deepseq instance for the diagrams.
> Does somebody has an idea on how to speed this up / get the diagram
> evaluated strictly as especially scrolling the DrawingArea is now really
> a pain?

Cairo is thread safe* so you could render the whole thing (if it isn't
super huge dimensions) to an image surface in the background thread,
then displaying could be a matter of copying the correct part (for
scrolling) of the surface to the DrawingArea.

Something like this perhaps (untested, incomplete):

import qualified Diagrams.(...) as D
import qualified Graphics.Rendering.Cairo as C

renderDiaToSurface width height diagram = do
  let w' = fromIntegral width
      h' = fromIntegral height
      opts = D.CairoOptions "" (D.Dims w' h') D.RenderOnly False
      (_, render) = D.renderDia D.Cairo opts diagram
  surface <- C.createImageSurface C.FormatARGB32 width height
  C.renderWith surface render
  return surface


Hope this is useful,


* I had some very rare crashes in heavily threaded code, still not found
the root cause...


More information about the Haskell-Cafe mailing list