add some comments to Canvas::Image and ensure that the canvas redraws after a put_ima...
[ardour.git] / libs / canvas / image.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "canvas/image.h"
20
21 #include "gtkmm2ext/gui_thread.h"
22
23 using namespace ArdourCanvas;
24
25 Image::Image (Group* group, Cairo::Format fmt, int width, int height)
26         : Item (group)
27         , _format (fmt)
28         , _width (width)
29         , _height (height)
30         , _need_render (false)
31 {
32         DataReady.connect (data_connections, MISSING_INVALIDATOR, boost::bind (&Image::accept_data, this), gui_context());
33 }
34
35 void 
36 Image::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
37 {
38         if (_current) {
39                 _surface = Cairo::ImageSurface::create (_current->data.get(),
40                                                         _current->format,
41                                                         _current->width,
42                                                         _current->height,
43                                                         _current->stride);
44         }
45
46         _current.reset ();
47
48         context->set_source (_surface, 0, 0);
49         context->rectangle (area.x0, area.y0, area.width(), area.height());
50         context->fill ();
51 }
52
53 void
54 Image::compute_bounding_box () const
55 {
56         _bounding_box = boost::optional<Rect> (Rect (0, 0, _width, _height));
57         _bounding_box_dirty = false;
58 }
59
60 boost::shared_ptr<Image::Data>
61 Image::get_image ()
62 {
63         /* can be called by any thread */
64
65         int stride = Cairo::ImageSurface::format_stride_for_width (_format, _width);
66         boost::shared_ptr<Data> d (new Data (boost::shared_array<uint8_t> (new uint8_t[stride*_height]), _width, _height, stride, _format));
67
68         return d;
69 }
70
71 void
72 Image::put_image (boost::shared_ptr<Data> d)
73 {
74         /* can be called by any thread */
75
76         _pending = d;
77         DataReady (); /* EMIT SIGNAL */
78 }
79
80 void
81 Image::accept_data () 
82 {
83         /* must be executed in gui thread */
84
85         begin_change ();
86
87         _current = _pending;
88         _pending.reset ();
89         _need_render = true;
90
91         end_change (); // notify canvas that we need redrawing
92 }            
93