fix crash when copy'ing latent plugins
[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 (Canvas* canvas, Cairo::Format fmt, int width, int height)
26         : Item (canvas)
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 Image::Image (Item* parent, Cairo::Format fmt, int width, int height)
36         : Item (parent)
37         , _format (fmt)
38         , _width (width)
39         , _height (height)
40         , _need_render (false)
41 {
42         DataReady.connect (data_connections, MISSING_INVALIDATOR, boost::bind (&Image::accept_data, this), gui_context());
43 }
44
45 void
46 Image::render (Rect const& area, Cairo::RefPtr<Cairo::Context> context) const
47 {
48         if (_need_render && _pending) {
49                 _surface = Cairo::ImageSurface::create (_pending->data,
50                                                         _pending->format,
51                                                         _pending->width,
52                                                         _pending->height,
53                                                         _pending->stride);
54                 _current = _pending;
55         }
56
57         Rect self = item_to_window (Rect (0, 0, _width, _height));
58         boost::optional<Rect> draw = self.intersection (area);
59
60         if (_surface && draw) {
61                 context->set_source (_surface, self.x0, self.y0);
62                 context->rectangle (draw->x0, draw->y0, draw->width(), draw->height());
63                 context->fill ();
64         }
65 }
66
67 void
68 Image::compute_bounding_box () const
69 {
70         _bounding_box = boost::optional<Rect> (Rect (0, 0, _width, _height));
71         _bounding_box_dirty = false;
72 }
73
74 boost::shared_ptr<Image::Data>
75 Image::get_image (bool allocate_data)
76 {
77         /* can be called by any thread */
78
79         int stride = Cairo::ImageSurface::format_stride_for_width (_format, _width);
80         if (allocate_data)  {
81                 boost::shared_ptr<Data> d (new Data (new uint8_t[stride*_height], _width, _height, stride, _format));
82                 return d;
83         } else {
84                 boost::shared_ptr<Data> d (new Data (NULL, _width, _height, stride, _format));
85                 return d;
86         }
87 }
88
89 void
90 Image::put_image (boost::shared_ptr<Data> d)
91 {
92         /* can be called by any thread */
93
94         _pending = d;
95         DataReady (); /* EMIT SIGNAL */
96 }
97
98 void
99 Image::accept_data ()
100 {
101         /* must be executed in gui thread */
102
103         begin_change ();
104         _need_render = true;
105         end_change (); // notify canvas that we need redrawing
106 }
107