renice x-fade rendering w/cairo-antialiasing
[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/utils.h"
26 #include "canvas/xfade_curve.h"
27 #include "canvas/interpolated_curve.h"
28
29 using namespace ArdourCanvas;
30 using std::min;
31 using std::max;
32
33 XFadeCurve::XFadeCurve (Group* parent)
34         : Item (parent)
35         , points_per_segment (24)
36         , _xfadeposition (Start)
37         , _outline_color (0x000000ff)
38         , _fill_color (0x22448880)
39 {
40 }
41
42 XFadeCurve::XFadeCurve (Group* parent, XFadePosition pos)
43         : Item (parent)
44         , points_per_segment (24)
45         , _xfadeposition (pos)
46         , _outline_color (0x000000ff)
47         , _fill_color (0x22448880)
48 {
49 }
50
51 void
52 XFadeCurve::compute_bounding_box () const
53 {
54         if (!_in.points.empty() && !_out.points.empty()) {
55
56                 Rect bbox;
57                 Points::const_iterator i;
58
59                 if (!_in.points.empty()) {
60                         i = _in.points.begin();
61                         bbox.x0 = bbox.x1 = i->x;
62                         bbox.y0 = bbox.y1 = i->y;
63
64                         ++i;
65
66                         while (i != _in.points.end()) {
67                                 bbox.x0 = min (bbox.x0, i->x);
68                                 bbox.y0 = min (bbox.y0, i->y);
69                                 bbox.x1 = max (bbox.x1, i->x);
70                                 bbox.y1 = max (bbox.y1, i->y);
71                                 ++i;
72                         }
73                 } else {
74                         i = _out.points.begin();
75                         bbox.x0 = bbox.x1 = i->x;
76                         bbox.y0 = bbox.y1 = i->y;
77                 }
78
79                 if (!_out.points.empty()) {
80                         i = _out.points.begin();
81                         while (i != _out.points.end()) {
82                                 bbox.x0 = min (bbox.x0, i->x);
83                                 bbox.y0 = min (bbox.y0, i->y);
84                                 bbox.x1 = max (bbox.x1, i->x);
85                                 bbox.y1 = max (bbox.y1, i->y);
86                                 ++i;
87                         }
88                 }
89
90                 _bounding_box = bbox.expand (1.0);
91
92         } else {
93                 _bounding_box = boost::optional<Rect> ();
94         }
95
96         _bounding_box_dirty = false;
97 }
98
99 void
100 XFadeCurve::set_inout (Points const & in, Points const & out)
101 {
102         if (_in.points == in && _out.points == out) {
103                 return;
104         }
105         begin_change ();
106         _in.points = in;
107         _out.points = out;
108         _bounding_box_dirty = true;
109         interpolate ();
110         end_change ();
111 }
112
113 void
114 XFadeCurve::set_points_per_segment (uint32_t n)
115 {
116         points_per_segment = n;
117         interpolate ();
118         redraw ();
119 }
120
121 void
122 XFadeCurve::interpolate ()
123 {
124         _in.samples.clear ();
125         InterpolatedCurve::interpolate (_in.points, points_per_segment, CatmullRomCentripetal, false, _in.samples);
126         _in.n_samples = _in.samples.size();
127
128         _out.samples.clear ();
129         InterpolatedCurve::interpolate (_out.points, points_per_segment, CatmullRomCentripetal, false, _out.samples);
130         _out.n_samples = _out.samples.size();
131 }
132
133 Cairo::Path *
134 XFadeCurve::get_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c) const
135 {
136         assert(c.points.size() > 1);
137         context->begin_new_path ();
138         Duple window_space;
139
140         if (c.points.size () == 2) {
141
142                 window_space = item_to_window (c.points.front());
143                 context->move_to (window_space.x, window_space.y);
144                 window_space = item_to_window (c.points.back());
145                 context->line_to (window_space.x, window_space.y);
146
147         } else {
148
149                 /* find left and right-most sample */
150                 Points::size_type left = 0;
151                 Points::size_type right = c.n_samples;
152
153                 for (Points::size_type idx = 0; idx < c.n_samples - 1; ++idx) {
154                         left = idx;
155                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0));
156                         if (window_space.x >= area.x0) break;
157                 }
158                 for (Points::size_type idx = c.n_samples; idx > left + 1; --idx) {
159                         window_space = item_to_window (Duple (c.samples[idx].x, 0.0));
160                         if (window_space.x <= area.x1) break;
161                         right = idx;
162                 }
163
164                 /* draw line between samples */
165                 window_space = item_to_window (Duple (c.samples[left].x, c.samples[left].y));
166                 context->move_to (window_space.x, window_space.y);
167                 Coord last_x = round(window_space.x);
168                 Coord last_y = round(window_space.y);
169                 for (uint32_t idx = left + 1; idx < right; ++idx) {
170                         window_space = item_to_window (Duple (c.samples[idx].x, c.samples[idx].y));
171                         if (last_x == round(window_space.x)) continue;
172                         if (last_y == round(window_space.y)) continue;
173                         last_x = round(window_space.x);
174                         last_y = round(window_space.y);
175                         context->line_to (last_x - .5 , last_y + .5);
176                 }
177         }
178         return context->copy_path ();
179 }
180
181 void
182 XFadeCurve::close_path(Rect const & area, Cairo::RefPtr<Cairo::Context> context, CanvasCurve const &c, bool inside) const
183 {
184         Duple window_space;
185         if (inside) {
186                 window_space = item_to_window (Duple(c.points.back().x, area.height()));
187                 context->line_to (window_space.x, window_space.y);
188                 window_space = item_to_window (Duple(c.points.front().x, area.height()));
189                 context->line_to (window_space.x, window_space.y);
190                 context->close_path();
191         } else {
192                 window_space = item_to_window (Duple(c.points.back().x, 0.0));
193                 context->line_to (window_space.x, window_space.y);
194                 window_space = item_to_window (Duple(c.points.front().x, 0.0));
195                 context->line_to (window_space.x, window_space.y);
196                 context->close_path();
197         }
198 }
199
200 void
201 XFadeCurve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
202 {
203         if (!_bounding_box) { return; }
204         if (_in.points.size() < 2) { return; }
205         if (_out.points.size() < 2) { return; }
206
207         Rect self = item_to_window (_bounding_box.get());
208         boost::optional<Rect> d = self.intersection (area);
209         assert (d);
210         Rect draw = d.get ();
211
212         context->save ();
213         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
214         context->clip ();
215
216         /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
217          * they will still occur, but cairo's clipping will hide them.
218          */
219         draw = draw.expand (4.0);
220
221         Cairo::Path *path_in = get_path(draw, context, _in);
222         Cairo::Path *path_out = get_path(draw, context, _out);
223
224         Color outline_shaded = _outline_color;
225         outline_shaded = 0.5 * (outline_shaded & 0xff) + (outline_shaded & ~0xff);
226
227         Color fill_shaded = _fill_color;
228         fill_shaded = 0.5 * (fill_shaded & 0xff) + (fill_shaded & ~0xff);
229
230 #define IS (_xfadeposition == Start)
231
232         /* fill primary fade */
233         context->begin_new_path ();
234         context->append_path (IS ? *path_in : *path_out);
235         close_path(draw, context, IS ?_in : _out, false);
236         set_source_rgba (context, _fill_color);
237         context->fill ();
238
239         /* fill background fade */
240         context->save ();
241         context->begin_new_path ();
242         context->append_path (IS ? *path_in : *path_out);
243         close_path(draw, context, IS ? _in : _out, true);
244         //context->set_fill_rule (Cairo::FILL_RULE_EVEN_ODD);
245         context->clip ();
246         context->begin_new_path ();
247         context->append_path (IS ? *path_out: *path_in);
248         close_path(draw, context, IS ? _out : _in, true);
249         set_source_rgba (context, fill_shaded);
250         //context->set_fill_rule (Cairo::FILL_RULE_WINDING);
251         context->fill ();
252         context->restore ();
253
254         /* draw lines over fills */
255         set_source_rgba (context, IS ? _outline_color : outline_shaded);
256         context->set_line_width (IS ? 1.0 : .5);
257
258         context->begin_new_path ();
259         context->append_path (*path_in);
260         context->stroke();
261
262         set_source_rgba (context, IS ? outline_shaded :_outline_color);
263         context->set_line_width (IS ? .5 : 1.0);
264
265         context->begin_new_path ();
266         context->append_path (*path_out);
267         context->stroke();
268
269         context->restore ();
270
271         delete path_in;
272         delete path_out;
273 }