1 ! Copyright (C) 2008 Matthew Willis.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: cairo cairo.ffi ui.render kernel opengl.gl opengl
4 math byte-arrays ui.gadgets accessors arrays
5 namespaces io.backend ;
9 ! We need two kinds of gadgets:
10 ! one performs the cairo ops once and caches the bytes, the other
11 ! performs cairo ops every refresh
13 TUPLE: cairo-gadget width height quot cache? bytes ;
14 PREDICATE: cached-cairo < cairo-gadget cache?>> ;
15 : <cairo-gadget> ( width height quot -- cairo-gadget )
16 cairo-gadget construct-gadget
21 : <cached-cairo> ( width height quot -- cairo-gadget )
22 <cairo-gadget> t >>cache? ;
24 : width>stride ( width -- stride ) 4 * ;
26 : copy-cairo ( width height quot -- byte-array )
28 [ * nip <byte-array> dup CAIRO_FORMAT_ARGB32 ]
29 [ cairo_image_surface_create_for_data ] 3bi
30 r> with-cairo-from-surface ;
32 : (cairo>bytes) ( gadget -- byte-array )
33 [ width>> ] [ height>> ] [ quot>> ] tri copy-cairo ;
36 M: cairo-gadget cairo>bytes ( gadget -- byte-array )
39 M: cached-cairo cairo>bytes ( gadget -- byte-array )
41 dup (cairo>bytes) [ >>bytes drop ] keep
44 : cairo>png ( gadget path -- )
45 >r [ cairo>bytes CAIRO_FORMAT_ARGB32 ] [ width>> ]
46 [ height>> ] tri over width>stride
47 cairo_image_surface_create_for_data
48 r> [ cairo_surface_write_to_png check-cairo ] curry with-surface ;
50 M: cairo-gadget draw-gadget* ( gadget -- )
54 [ width>> ] [ height>> GL_BGRA GL_UNSIGNED_BYTE ]
55 [ cairo>bytes ] tri glDrawPixels
58 M: cairo-gadget pref-dim* ( gadget -- rect )
59 [ width>> ] [ height>> ] bi 2array ;
61 : copy-surface ( surface -- )
62 cr swap 0 0 cairo_set_source_surface
65 : <bytes-gadget> ( width height bytes -- cairo-gadget )
66 >r [ ] <cached-cairo> r> >>bytes ;
68 : <png-gadget> ( path -- gadget )
69 normalize-path cairo_image_surface_create_from_png
70 [ cairo_image_surface_get_width ]
71 [ cairo_image_surface_get_height 2dup ]
72 [ [ copy-surface ] curry copy-cairo ] tri