0c7ce6eb28b190f1e96f7ddb7807d319f706e4f9
[ardour.git] / libs / canvas / image.cc
1 #include "canvas/image.h"
2
3 #include "gtkmm2ext/gui_thread.h"
4
5 #include "pbd/xml++.h"
6
7 using namespace ArdourCanvas;
8
9 Image::Image (Group* group, Cairo::Format fmt, int width, int height)
10         : Item (group)
11         , _format (fmt)
12         , _width (width)
13         , _height (height)
14         , _need_render (false)
15 {
16         DataReady.connect (data_connections, MISSING_INVALIDATOR, boost::bind (&Image::accept_data, this), gui_context());
17 }
18
19 void 
20 Image::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
21 {
22         if (_current) {
23                 _surface = Cairo::ImageSurface::create (_current->data.get(),
24                                                         _current->format,
25                                                         _current->width,
26                                                         _current->height,
27                                                         _current->stride);
28         }
29
30         _current.reset ();
31
32         context->set_source (_surface, 0, 0);
33         context->rectangle (area.x0, area.y0, area.width(), area.height());
34         context->fill ();
35 }
36
37 void
38 Image::compute_bounding_box () const
39 {
40         _bounding_box = boost::optional<Rect> (Rect (0, 0, _width, _height));
41         _bounding_box_dirty = false;
42 }
43
44 boost::shared_ptr<Image::Data>
45 Image::get_image ()
46 {
47         int stride = Cairo::ImageSurface::format_stride_for_width (_format, _width);
48         boost::shared_ptr<Data> d (new Data (boost::shared_array<uint8_t> (new uint8_t[stride*_height]), _width, _height, stride, _format));
49
50         return d;
51 }
52
53 void
54 Image::put_image (boost::shared_ptr<Data> d)
55 {
56         _pending = d;
57         DataReady (); /* EMIT SIGNAL */
58 }
59
60 void
61 Image::accept_data () 
62 {
63         /* must be executed in gui thread */
64         _current = _pending;
65         _pending.reset ();
66         _need_render = true;
67 }            
68
69 XMLNode *
70 Image::get_state () const
71 {
72         /* XXX */
73         return new XMLNode ("Image");
74 }
75
76 void
77 Image::set_state (XMLNode const * /*node*/)
78 {
79         /* XXX */
80 }