Use a dumb cache for waveview ImageSurfaces. This halves the size of the
[ardour.git] / libs / canvas / group.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
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 #include <iostream>
21 #include <cairomm/context.h>
22
23 #include "pbd/stacktrace.h"
24 #include "pbd/compose.h"
25
26 #include "canvas/group.h"
27 #include "canvas/types.h"
28 #include "canvas/debug.h"
29 #include "canvas/item.h"
30 #include "canvas/canvas.h"
31
32 using namespace std;
33 using namespace ArdourCanvas;
34
35 int Group::default_items_per_cell = 64;
36
37
38 Group::Group (Canvas* canvas)
39         : Item (canvas)
40         , _lut (0)
41 {
42         
43 }
44
45 Group::Group (Group* parent)
46         : Item (parent)
47         , _lut (0)
48 {
49         
50 }
51
52 Group::Group (Group* parent, Duple position)
53         : Item (parent, position)
54         , _lut (0)
55 {
56         
57 }
58
59 Group::~Group ()
60 {
61         clear_items (true);
62 }
63
64 /** @param area Area to draw in window coordinates.
65  *  @param context Context, set up with its origin at this group's position.
66  */
67 void
68 Group::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
69 {
70         ensure_lut ();
71         vector<Item*> items = _lut->get (area);
72
73 #ifdef CANVAS_DEBUG
74         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
75                 cerr << string_compose ("%1GROUP %2 @ %7 render %5 @ %6 %3 items out of %4\n", 
76                                         _canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size(), area, _position, this);
77         }
78 #endif
79
80         ++render_depth;
81                 
82         for (vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {
83
84                 if (!(*i)->visible ()) {
85 #ifdef CANVAS_DEBUG
86                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
87                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] invisible - skipped\n";
88                         }
89 #endif
90                         continue;
91                 }
92                 
93                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
94
95                 if (!item_bbox) {
96 #ifdef CANVAS_DEBUG
97                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
98                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] empty - skipped\n";
99                         }
100 #endif
101                         continue;
102                 }
103                 
104                 Rect item = (*i)->item_to_window (item_bbox.get());
105                 boost::optional<Rect> d = item.intersection (area);
106                 
107                 if (d) {
108                         Rect draw = d.get();
109                         if (draw.width() && draw.height()) {
110 #ifdef CANVAS_DEBUG
111                                 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
112                                         if (dynamic_cast<Group*>(*i) == 0) {
113                                                 cerr << _canvas->render_indent() << "render "
114                                                      << ' ' 
115                                                      << (*i)
116                                                      << ' '
117                                                      << (*i)->whatami()
118                                                      << ' '
119                                                      << (*i)->name
120                                                      << " item "
121                                                      << item_bbox.get()
122                                                      << " window = " 
123                                                      << item
124                                                      << " intersect = "
125                                                      << draw
126                                                      << " @ " 
127                                                      << _position
128                                                      << endl;
129                                         }
130                                 }
131 #endif
132
133                                 (*i)->render (area, context);
134                                 ++render_count;
135                         }
136
137                 } else {
138
139 #ifdef CANVAS_DEBUG
140                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
141                                 cerr << string_compose ("%1skip render of %2 %3, no intersection between %4 and %5\n", _canvas->render_indent(), (*i)->whatami(),
142                                                         (*i)->name, item, area);
143                         }
144 #endif
145
146                 }
147         }
148
149         --render_depth;
150 }
151
152 void
153 Group::scroll_to (Duple const& d)
154 {
155         Item::scroll_to (d);
156         
157         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
158                 (*i)->scroll_to (d);
159         }
160 }
161
162 void
163 Group::compute_bounding_box () const
164 {
165         Rect bbox;
166         bool have_one = false;
167
168         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
169
170                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
171
172                 if (!item_bbox) {
173                         continue;
174                 }
175
176                 Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
177                 if (have_one) {
178                         bbox = bbox.extend (group_bbox);
179                 } else {
180                         bbox = group_bbox;
181                         have_one = true;
182                 }
183         }
184
185         if (!have_one) {
186                 _bounding_box = boost::optional<Rect> ();
187         } else {
188                 _bounding_box = bbox;
189         }
190
191         _bounding_box_dirty = false;
192 }
193
194 void
195 Group::add (Item* i)
196 {
197         /* XXX should really notify canvas about this */
198
199         _items.push_back (i);
200         invalidate_lut ();
201         _bounding_box_dirty = true;
202
203         
204 }
205
206 void
207 Group::remove (Item* i)
208 {
209
210         if (i->parent() != this) {
211                 return;
212         }
213
214         /* we cannot call bounding_box() here because that will iterate over
215            _items, one of which (the argument, i) may be in the middle of
216            deletion, making it impossible to call compute_bounding_box()
217            on it.
218         */
219
220         if (_bounding_box) {
221                 _pre_change_bounding_box = _bounding_box;
222         } else {
223                 _pre_change_bounding_box = Rect();
224         }
225
226         i->unparent ();
227         _items.remove (i);
228         invalidate_lut ();
229         _bounding_box_dirty = true;
230         
231         end_change ();
232 }
233
234 void
235 Group::clear (bool with_delete)
236 {
237         begin_change ();
238
239         clear_items (with_delete);
240
241         invalidate_lut ();
242         _bounding_box_dirty = true;
243
244         end_change ();
245 }
246
247 void
248 Group::clear_items (bool with_delete)
249 {
250         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ) {
251
252                 list<Item*>::iterator tmp = i;
253                 Item *item = *i;
254
255                 ++tmp;
256
257                 /* remove from list before doing anything else, because we
258                  * don't want to find the item in _items during any activity
259                  * driven by unparent-ing or deletion.
260                  */
261
262                 _items.erase (i);
263                 item->unparent ();
264                 
265                 if (with_delete) {
266                         delete item;
267                 }
268
269                 i = tmp;
270         }
271 }
272
273 void
274 Group::raise_child_to_top (Item* i)
275 {
276         if (!_items.empty()) {
277                 if (_items.back() == i) {
278                         return;
279                 }
280         }
281
282         _items.remove (i);
283         _items.push_back (i);
284         invalidate_lut ();
285 }
286
287 void
288 Group::raise_child (Item* i, int levels)
289 {
290         list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
291         assert (j != _items.end ());
292
293         ++j;
294         _items.remove (i);
295
296         while (levels > 0 && j != _items.end ()) {
297                 ++j;
298                 --levels;
299         }
300
301         _items.insert (j, i);
302         invalidate_lut ();
303 }
304
305 void
306 Group::lower_child_to_bottom (Item* i)
307 {
308         if (!_items.empty()) {
309                 if (_items.front() == i) {
310                         return;
311                 }
312         }
313         _items.remove (i);
314         _items.push_front (i);
315         invalidate_lut ();
316 }
317
318 void
319 Group::ensure_lut () const
320 {
321         if (!_lut) {
322                 _lut = new DumbLookupTable (*this);
323         }
324 }
325
326 void
327 Group::invalidate_lut () const
328 {
329         delete _lut;
330         _lut = 0;
331 }
332
333 void
334 Group::child_changed ()
335 {
336         invalidate_lut ();
337         _bounding_box_dirty = true;
338
339         if (_parent) {
340                 _parent->child_changed ();
341         }
342 }
343
344 void
345 Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
346 {
347         boost::optional<Rect> const bbox = bounding_box ();
348
349         /* Point is in window coordinate system */
350
351         if (!bbox || !item_to_window (bbox.get()).contains (point)) {
352                 return;
353         }
354
355         /* now recurse and add any items within our group that contain point */
356
357         ensure_lut ();
358         vector<Item*> our_items = _lut->items_at_point (point);
359
360         if (!our_items.empty()) {
361                 /* this adds this group itself to the list of items at point */
362                 Item::add_items_at_point (point, items);
363         }
364
365         for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
366                 (*i)->add_items_at_point (point, items);
367         }
368 }
369
370 void
371 Group::dump (ostream& o) const
372 {
373 #ifdef CANVAS_DEBUG
374         o << _canvas->indent();
375         o << "Group " << this << " [" << name << ']';
376         o << " @ " << position();
377         o << " Items: " << _items.size();
378         o << " Visible ? " << _visible;
379
380         boost::optional<Rect> bb = bounding_box();
381
382         if (bb) {
383                 o << endl << _canvas->indent() << "  bbox: " << bb.get();
384                 o << endl << _canvas->indent() << "  CANVAS bbox: " << item_to_canvas (bb.get());
385         } else {
386                 o << "  bbox unset";
387         }
388
389         o << endl;
390 #endif
391
392         ArdourCanvas::dump_depth++;
393
394         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
395                 o << **i;
396         }
397
398         ArdourCanvas::dump_depth--;
399 }