fix crash when copy'ing latent plugins
[ardour.git] / libs / canvas / xfade_curve.cc
1 /*
2   Copyright (C) 2013 Paul Davis
3   Copyright (C) 2014 Robin Gareus <robin@gareus.org>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <cmath>
22 #include <exception>
23 #include <algorithm>
24
25 #include "canvas/xfade_curve.h"
26 #include "canvas/interpolated_curve.h"
27 #include "canvas/utils.h"
28
29 using namespace ArdourCanvas;
30 using std::min;
31 using std::max;
32
33 #ifdef USE_TRACKS_CODE_FEATURES
34 static const bool show_bg_fades = false;
35 #else
36 static const bool show_bg_fades = true;
37 #endif
38
39 XFadeCurve::XFadeCurve (Canvas* c)
40         : Item (c)
41         , points_per_segment (32)
42         , _xfadeposition (Start)
43         , _outline_color (0x000000ff)
44         , _fill_color (0x22448880)
45         , show_background_fade (show_bg_fades)
46 {
47 }
48
49 XFadeCurve::XFadeCurve (Canvas* c, XFadePosition pos)
50         : Item (c)
51         , points_per_segment (32)
52         , _xfadeposition (pos)
53         , _outline_color (0x000000ff)
54         , _fill_color (0x22448880)
55         , show_background_fade (show_bg_fades)
56 {
57 }
58
59 XFadeCurve::XFadeCurve (Item* parent)
60         : Item (parent)
61         , points_per_segment (32)
62         , _xfadeposition (Start)
63         , _outline_color (0x000000ff)
64         , _fill_color (0x22448880)
65         , show_background_fade (show_bg_fades)
66 {
67 }
68
69 XFadeCurve::XFadeCurve (Item* parent, XFadePosition pos)
70         : Item (parent)
71         , points_per_segment (32)
72         , _xfadeposition (pos)
73         , _outline_color (0x000000ff)
74         , _fill_color (0x22448880)
75         , show_background_fade (show_bg_fades)
76 {
77 }
78
79 void
80 XFadeCurve::compute_bounding_box () const
81 {
82         if (!_in.points.empty() && !_out.points.empty()) {
83
84                 Rect bbox;
85                 Points::const_iterator i;
86
87                 if (!_in.points.empty()) {
88                         i = _in.points.begin();
89                         bbox.x0 = bbox.x1 = i->x;
90                         bbox.y0 = bbox.y1 = i->y;
91
92                         ++i;
93
94                         while (i != _in.points.end()) {
95                                 bbox.x0 = min (bbox.x0, i->x);
96                                 bbox.y0 = min (bbox.y0, i->y);
97                                 bbox.x1 = max (bbox.x1, i->x);
98                                 bbox.y1 = max (bbox.y1, i->y);
99                                 ++i;
100                         }
101                 } else {
102                         i = _out.points.begin();
103                         bbox.x0 = bbox.x1 = i->x;
104                         bbox.y0 = bbox.y1 = i->y;
105                 }
106
107                 if (!_out.points.empty()) {
108                         i = _out.points.begin();
109                         while (i != _out.points.end()) {
110                                 bbox.x0 = min (bbox.x0, i->x);
111                                 bbox.y0 = min (bbox.y0, i->y);
112                                 bbox.x1 = max (bbox.x1, i->x);
113                                 bbox.y1 = max (bbox.y1, i->y);
114                                 ++i;
115                         }
116                 }
117
118                 _bounding_box = bbox.expand (1.0);
119
120         } else {
121                 _bounding_box = boost::optional<Rect> ();
122         }
123
124         _bounding_box_dirty = false;
125 }
126
127 void
128 XFadeCurve::set_inout (Points const & in, Points const & out)
129 {
130         if (_in.points == in && _out.points == out) {
131                 return;
132         }
133         begin_change ();
134         _in.points = in;
135         _out.points = out;
136         _bounding_box_dirty = true;
137         interpolate ();
138         end_change ();
139 }
140
141 void
142 XFadeCurve::set_points_per_segment (uint32_t n)
143 {
144         points_per_segment = n;
145         interpolate ();
146         redraw ();
147 }
148
149 void
150 XFadeCurve::interpolate ()
151 {
152         _in.samples.clear ();
153         InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
154         _in.n_samples = _in.samples.size();
155
156         _out.samples.clear ();
157         InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
158         _out.n_samples = _out.samples.size();
159 }
160
161 Cairo::Path *
162 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
163 {
164         assert(c.points.size() > 1);
165         context->begin_new_path ();
166         Duple window_space;
167
168         if (c.points.size () == 2) {
169
170                 window_space = item_to_window (c.points.front(), false);
171                 context->move_to (window_space.x, window_space.y);
172                 window_space = item_to_window (c.points.back(), false);
173                 context->line_to (window_space.x, window_space.y);
174
175         } else {
176
177                 /* find left and right-most sample */
178                 Points::size_type left = 0;
179                 Points::size_type right = c.n_samples - 1;
180
181                 assert (left < right);
182                 // we should really really do a binary search rather than iterate
183                 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
184                         left = idx;
185                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
186                         if (window_space.x >= area.x0) break;
187                 }
188                 for (Points::size_type idx = c.n_samples - 1; right > left;) {
189                         if (--idx <= left) break;
190                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0), false);
191                         if (window_space.x <= area.x1) break;
192                         right = idx;
193                 }
194
195                 assert(left < right);
196                 assert(left < c.n_samples);
197                 assert(right < c.n_samples);
198
199                 /* draw line between samples */
200                 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y), false);
201                 context->move_to (window_space.x, window_space.y);
202                 for (uint32_t idx = left + 1; idx <= right; ++idx) {
203                         window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y), false);
204                         context->line_to (window_space.x, window_space.y);
205                 }
206         }
207         return context->copy_path ();
208 }
209
210 void
211 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
212 {
213         Duple window_space;
214         if (inside) {
215                 window_space = item_to_window (Duple(c.points.back().x, area.height()), false);
216                 context->line_to (window_space.x, window_space.y);
217                 window_space = item_to_window (Duple(c.points.front().x, area.height()), false);
218                 context->line_to (window_space.x, window_space.y);
219                 context->close_path();
220         } else {
221                 window_space = item_to_window (Duple(c.points.back().x, 0.0), false);
222                 context->line_to (window_space.x, window_space.y);
223                 window_space = item_to_window (Duple(c.points.front().x, 0.0), false);
224                 context->line_to (window_space.x, window_space.y);
225                 context->close_path();
226         }
227 }
228
229 void
230 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
231 {
232         if (!_bounding_box) { return; }
233         if (_in.points.size() < 2) { return; }
234         if (_out.points.size() < 2) { return; }
235
236         Rect self = item_to_window (_bounding_box.get());
237         boost::optional<Rect> d = self.intersection (area);
238         assert (d);
239         Rect draw = d.get ();
240
241         context->save ();
242         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
243         context->clip ();
244
245         /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
246          * they will still occur, but cairo's clipping will hide them.
247          */
248         draw = draw.expand (4.0);
249
250         Cairo::Path *path_in = get_path(draw, context, _in);
251         Cairo::Path *path_out = get_path(draw, context, _out);
252
253         Color outline_shaded = _outline_color;
254         outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
255
256         Color fill_shaded = _fill_color;
257         fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
258
259 #define IS_START (_xfadeposition == Start)
260
261         /* fill primary fade */
262         context->begin_new_path ();
263         context->append_path (IS_START ? *path_in : *path_out);
264         close_path(draw, context, IS_START ?_in : _out, false);
265         set_source_rgba (context, _fill_color);
266         context->fill ();
267
268         if (show_background_fade) {
269                 /* fill background fade */
270                 context->save ();
271                 context->begin_new_path ();
272                 context->append_path (IS_START ? *path_in : *path_out);
273                 close_path(draw, context, IS_START ? _in : _out, true);
274                 context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
275                 context->clip ();
276                 context->begin_new_path ();
277                 context->append_path (IS_START ? *path_out: *path_in);
278                 close_path(draw, context, IS_START ? _out : _in, true);
279                 set_source_rgba (context, fill_shaded);
280                 context->set_fill_rule (Cairo::FILL_RULE_WINDING);
281                 context->fill ();
282                 context->restore ();
283         }
284
285         /* draw lines over fills */
286         /* fade in line */
287         if (IS_START || show_background_fade) {
288                 set_source_rgba (context, IS_START ? _outline_color : outline_shaded);
289                 context->set_line_width (IS_START ? 1.0 : .5);
290
291                 context->begin_new_path ();
292                 context->append_path (*path_in);
293                 context->stroke();
294         }
295
296         /* fade out line */
297         if (!IS_START || show_background_fade) {
298                 set_source_rgba (context, IS_START ? outline_shaded :_outline_color);
299                 context->set_line_width (IS_START ? .5 : 1.0);
300
301                 context->begin_new_path ();
302                 context->append_path (*path_out);
303                 context->stroke();
304         }
305
306         context->restore ();
307
308         delete path_in;
309         delete path_out;
310 }