Don't use the optimization of using STRAIGHT blends instead of COMPOSITE when SYNFIG_...
[synfig.git] / synfig-core / trunk / src / synfig / layer_pastecanvas.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_pastecanvas.cpp
3 **      \brief Implementation of the "Paste Canvas" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "layer_pastecanvas.h"
34 #include "string.h"
35 #include "time.h"
36 #include "context.h"
37 #include "paramdesc.h"
38 #include "renddesc.h"
39 #include "surface.h"
40 #include "value.h"
41 #include "valuenode.h"
42 #include "canvas.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace etl;
49 using namespace std;
50 using namespace synfig;
51
52 /* === M A C R O S ========================================================= */
53
54 #define MAX_DEPTH 10
55
56 // if this isn't defined, the 'dead heads' in examples/pirates.sifz don't render properly
57 #define SYNFIG_CLIP_PASTECANVAS
58
59 //#ifdef __APPLE__
60 //#undef SYNFIG_CLIP_PASTECANVAS
61 //#endif
62
63 /* === C L A S S E S ======================================================= */
64
65 class depth_counter     // Makes our recursive depth counter exception-safe
66 {
67         int *depth;
68 public:
69         depth_counter(int &x):depth(&x) { (*depth)++; }
70         ~depth_counter() { (*depth)--; }
71 };
72
73 /* === G L O B A L S ======================================================= */
74
75 SYNFIG_LAYER_INIT(Layer_PasteCanvas);
76 SYNFIG_LAYER_SET_NAME(Layer_PasteCanvas,"PasteCanvas"); // todo: use paste_canvas
77 SYNFIG_LAYER_SET_LOCAL_NAME(Layer_PasteCanvas,N_("Paste Canvas"));
78 SYNFIG_LAYER_SET_CATEGORY(Layer_PasteCanvas,N_("Other"));
79 SYNFIG_LAYER_SET_VERSION(Layer_PasteCanvas,"0.1");
80 SYNFIG_LAYER_SET_CVS_ID(Layer_PasteCanvas,"$Id$");
81
82 /* === M E T H O D S ======================================================= */
83
84 Layer_PasteCanvas::Layer_PasteCanvas():
85         origin(0,0),
86         depth(0),
87         zoom(0),
88         time_offset(0)
89 {
90         children_lock=false;
91         muck_with_time_=true;
92         curr_time=Time::begin();
93 }
94
95 Layer_PasteCanvas::~Layer_PasteCanvas()
96 {
97 /*      if(canvas)
98                 canvas->parent_set.erase(this);
99 */
100
101         //if(canvas)DEBUGINFO(strprintf("%d",canvas->count()));
102
103         set_sub_canvas(0);
104
105         //if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
106         //      canvas->unref();
107 }
108
109 String
110 Layer_PasteCanvas::get_local_name()const
111 {
112         if(!canvas)     return _("Pasted Canvas");
113         if(canvas->is_inline()) return _("Inline Canvas");
114         if(canvas->get_root()==get_canvas()->get_root()) return '[' + canvas->get_id() + ']';
115
116         return '[' + canvas->get_file_name() + ']';
117 }
118
119 Layer::Vocab
120 Layer_PasteCanvas::get_param_vocab()const
121 {
122         Layer::Vocab ret(Layer_Composite::get_param_vocab());
123
124         ret.push_back(ParamDesc("origin")
125                 .set_local_name(_("Origin"))
126                 .set_description(_("Point where you want the origin to be"))
127         );
128         ret.push_back(ParamDesc("canvas")
129                 .set_local_name(_("Canvas"))
130                 .set_description(_("Canvas to paste"))
131         );
132         ret.push_back(ParamDesc("zoom")
133                 .set_local_name(_("Zoom"))
134                 .set_description(_("Size of canvas"))
135         );
136
137         ret.push_back(ParamDesc("time_offset")
138                 .set_local_name(_("Time Offset"))
139         );
140
141         ret.push_back(ParamDesc("children_lock")
142                 .set_local_name(_("Children Lock"))
143         );
144
145         return ret;
146 }
147
148 bool
149 Layer_PasteCanvas::set_param(const String & param, const ValueBase &value)
150 {
151         IMPORT(origin);
152
153         // IMPORT(canvas);
154         if(param=="canvas" && value.same_type_as(Canvas::Handle()))
155         {
156                 set_sub_canvas(value.get(Canvas::Handle()));
157                 return true;
158         }
159
160         //! \todo this introduces bug 1844764 if enabled; it was introduced in r954, but I can't see if it's useful
161 #if 0
162         if (param=="time_offset" && value.same_type_as(time_offset))
163         {
164                 if (time_offset != value.get(Time()))
165                 {
166                         value.put(&time_offset);
167                         // notify that the time_offset has changed so we can update the
168                         // waypoint positions in parent layers
169                         changed();
170                 }
171                 return true;
172         }
173 #else
174         IMPORT(time_offset);
175 #endif
176
177         IMPORT(children_lock);
178         IMPORT(zoom);
179
180         return Layer_Composite::set_param(param,value);
181 }
182
183 void
184 Layer_PasteCanvas::set_sub_canvas(etl::handle<synfig::Canvas> x)
185 {
186         if(canvas && muck_with_time_)
187                 remove_child(canvas.get());
188
189         if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
190                 canvas->unref();
191
192         child_changed_connection.disconnect();
193
194         canvas=x;
195
196         /*if(canvas)
197                 child_changed_connection=canvas->signal_changed().connect(
198                         sigc::mem_fun(
199                                 *this,
200                                 &Layer_PasteCanvas::changed
201                         )
202                 );
203         */
204         if(canvas)
205                 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
206
207         if(canvas && muck_with_time_)
208                 add_child(canvas.get());
209
210         if(canvas && (canvas->is_inline() || !get_canvas() || get_canvas()->get_root()!=canvas->get_root()))
211                 canvas->ref();
212
213         if(canvas)
214                 on_canvas_set();
215 }
216
217 // This is called whenever the parent canvas gets set/changed
218 void
219 Layer_PasteCanvas::on_canvas_set()
220 {
221         //synfig::info("before count()=%d",count());
222         if(get_canvas() && canvas && canvas->is_inline() && canvas->parent()!=get_canvas())
223         {
224                 //synfig::info("during count()=%d",count());
225                 canvas->set_inline(get_canvas());
226         }
227         //synfig::info("after count()=%d",count());
228 }
229
230 ValueBase
231 Layer_PasteCanvas::get_param(const String& param)const
232 {
233         EXPORT(origin);
234         EXPORT(canvas);
235         EXPORT(zoom);
236         EXPORT(time_offset);
237         EXPORT(children_lock);
238
239         EXPORT_NAME();
240         EXPORT_VERSION();
241
242         return Layer_Composite::get_param(param);
243 }
244
245 void
246 Layer_PasteCanvas::set_time(Context context, Time time)const
247 {
248         if(depth==MAX_DEPTH)return;depth_counter counter(depth);
249         curr_time=time;
250
251         context.set_time(time);
252         if(canvas)
253         {
254                 canvas->set_time(time+time_offset);
255
256                 bounds=(canvas->get_context().get_full_bounding_rect()-canvas->rend_desc().get_focus())*exp(zoom)+origin+canvas->rend_desc().get_focus();
257         }
258         else
259                 bounds=Rect::zero();
260 }
261
262 synfig::Layer::Handle
263 Layer_PasteCanvas::hit_check(synfig::Context context, const synfig::Point &pos)const
264 {
265         if(depth==MAX_DEPTH)return 0;depth_counter counter(depth);
266
267         if (canvas) {
268                 Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
269
270                 if(canvas && get_amount() && canvas->get_context().get_color(target_pos).get_a()>=0.25)
271                 {
272                         if(!children_lock)
273                         {
274                                 return canvas->get_context().hit_check(target_pos);
275                         }
276                         return const_cast<Layer_PasteCanvas*>(this);
277                 }
278         }
279         return context.hit_check(pos);
280 }
281
282 Color
283 Layer_PasteCanvas::get_color(Context context, const Point &pos)const
284 {
285         if(!canvas || !get_amount())
286                 return context.get_color(pos);
287
288         if(depth==MAX_DEPTH)return Color::alpha();depth_counter counter(depth);
289
290         Point target_pos=(pos-canvas->rend_desc().get_focus()-origin)/exp(zoom)+canvas->rend_desc().get_focus();
291
292         return Color::blend(canvas->get_context().get_color(target_pos),context.get_color(pos),get_amount(),get_blend_method());
293 }
294
295
296 bool
297 Layer_PasteCanvas::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
298 {
299         if(cb && !cb->amount_complete(0,10000)) return false;
300
301         if(depth==MAX_DEPTH)
302                 // if we are at the extent of our depth,
303                 // then we should just return whatever is under us.
304                 return context.accelerated_render(surface,quality,renddesc,cb);
305
306         depth_counter counter(depth);
307
308         if(!canvas || !get_amount())
309                 return context.accelerated_render(surface,quality,renddesc,cb);
310
311         if(muck_with_time_ && curr_time!=Time::begin() && canvas->get_time()!=curr_time+time_offset)
312                 canvas->set_time(curr_time+time_offset);
313
314         SuperCallback stageone(cb,0,4500,10000);
315         SuperCallback stagetwo(cb,4500,9000,10000);
316         SuperCallback stagethree(cb,9000,9999,10000);
317
318         RendDesc desc(renddesc);
319         Vector::value_type zoomfactor=1.0/exp(zoom);
320         desc.clear_flags();
321         desc.set_tl((desc.get_tl()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
322         desc.set_br((desc.get_br()-canvas->rend_desc().get_focus()-origin)*zoomfactor+canvas->rend_desc().get_focus());
323         desc.set_flags(RendDesc::PX_ASPECT);
324
325         if (is_solid_color() || context->empty())
326         {
327                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
328                 surface->clear();
329         }
330         else if (!context.accelerated_render(surface,quality,renddesc,&stageone))
331                 return false;
332
333         Color::BlendMethod blend_method(get_blend_method());
334         const Rect full_bounding_rect(canvas->get_context().get_full_bounding_rect());
335
336         if(context->empty())
337         {
338                 if (Color::is_onto(blend_method)) return true;
339                 if (blend_method==Color::BLEND_COMPOSITE) blend_method=Color::BLEND_STRAIGHT;
340         }
341
342         if (!etl::intersect(context.get_full_bounding_rect(),full_bounding_rect+origin))
343         {
344                 if (Color::is_onto(blend_method)) return true;
345
346                 /* 'straight' is faster than 'composite' and has the same
347                  * effect if the affected area of the lower layer is
348                  * transparent;  however, if we're not clipping the blit to
349                  * just the bounding rectangle, the affected area is the whole
350                  * tile, so we can't use this optimisation
351                  */
352 #ifdef SYNFIG_CLIP_PASTECANVAS
353                 if (blend_method==Color::BLEND_COMPOSITE) blend_method=Color::BLEND_STRAIGHT;
354 #endif  // SYNFIG_CLIP_PASTECANVAS
355         }
356
357 #ifdef SYNFIG_CLIP_PASTECANVAS
358         Rect area(desc.get_rect() & full_bounding_rect);
359
360         Point min(area.get_min());
361         Point max(area.get_max());
362
363         if (desc.get_tl()[0] > desc.get_br()[0]) swap(min[0], max[0]);
364         if (desc.get_tl()[1] > desc.get_br()[1]) swap(min[1], max[1]);
365
366         const int x(floor_to_int((min[0] - desc.get_tl()[0]) / desc.get_pw()));
367         const int y(floor_to_int((min[1] - desc.get_tl()[1]) / desc.get_ph()));
368         const int w( ceil_to_int((max[0] - desc.get_tl()[0]) / desc.get_pw()) - x);
369         const int h( ceil_to_int((max[1] - desc.get_tl()[1]) / desc.get_ph()) - y);
370
371         desc.set_subwindow(x,y,w,h);
372
373         // \todo this used to also have "area.area()<=0.000001 || " - is it useful?
374         //               it was causing bug #1809480 (Zoom in beyond 8.75 in nested canvases fails)
375         if(desc.get_w()==0 || desc.get_h()==0)
376         {
377                 if(cb && !cb->amount_complete(10000,10000)) return false;
378                 return true;
379         }
380 #endif  // SYNFIG_CLIP_PASTECANVAS
381
382         // render the canvas to be pasted onto pastesurface
383         Surface pastesurface;
384         if(!canvas->get_context().accelerated_render(&pastesurface,quality,desc,&stagetwo))
385                 return false;
386
387 #ifdef SYNFIG_CLIP_PASTECANVAS
388         Surface::alpha_pen apen(surface->get_pen(x,y));
389 #else  // SYNFIG_CLIP_PASTECANVAS
390         Surface::alpha_pen apen(surface->begin());
391 #endif  // SYNFIG_CLIP_PASTECANVAS
392
393         apen.set_alpha(get_amount());
394         apen.set_blend_method(blend_method);
395         pastesurface.blit_to(apen);
396
397         if(cb && !cb->amount_complete(10000,10000)) return false;
398
399         return true;
400 }
401
402 Rect
403 Layer_PasteCanvas::get_bounding_rect()const
404 {
405         return bounds;
406 }
407
408 void Layer_PasteCanvas::get_times_vfunc(Node::time_set &set) const
409 {
410         Node::time_set tset;
411         if(canvas) tset = canvas->get_times();
412
413         Node::time_set::iterator i = tset.begin(), end = tset.end();
414
415         //Make sure we offset the time...
416         //! \todo: SOMETHING STILL HAS TO BE DONE WITH THE OTHER DIRECTION
417         //                 (recursing down the tree needs to take this into account too...)
418         for(; i != end; ++i)
419                 set.insert(*i
420 #ifdef ADJUST_WAYPOINTS_FOR_TIME_OFFSET // see node.h
421                                    - time_offset
422 #endif
423                         );
424
425         Layer::get_times_vfunc(set);
426 }