Documentation for the layer composite
[synfig.git] / synfig-core / src / synfig / layer_composite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_composite.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2008 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_composite.h"
34 #include "context.h"
35 #include "time.h"
36 #include "color.h"
37 #include "surface.h"
38 #include "renddesc.h"
39 #include "target.h"
40
41 #include "layer_bitmap.h"
42
43 #include "general.h"
44 #include "render.h"
45 #include "paramdesc.h"
46
47 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54
55 /* === M A C R O S ========================================================= */
56
57 /* === G L O B A L S ======================================================= */
58
59 /* === P R O C E D U R E S ================================================= */
60
61 /* === M E T H O D S ======================================================= */
62
63 bool
64 Layer_Composite::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc_, ProgressCallback *cb)  const
65 {
66         RendDesc renddesc(renddesc_);
67
68         if(!amount_)
69                 return context.accelerated_render(surface,quality,renddesc,cb);
70
71         CanvasBase image;
72
73         SuperCallback stageone(cb,0,50000,100000);
74         SuperCallback stagetwo(cb,50000,100000,100000);
75
76         Layer_Bitmap::Handle surfacelayer(new class Layer_Bitmap());
77
78         Context iter;
79
80         for(iter=context;*iter;iter++)
81                 image.push_back(*iter);
82
83         image.push_front(surfacelayer.get());
84
85         // We want to go ahead and schedule any other
86         // layers...
87 //      while(dynamic_cast<Layer_Composite*>(context->get()))
88 //      while(context->get() &&
89 //              &context->get()->AcceleratedRender==
90 //              &Layer_Composite::AcceleratedRender)
91 //              image.push_back(*(context++));
92
93         image.push_back(0);     // Alpha black
94
95         // Render the backdrop on the surface layer's surface.
96         if(!context.accelerated_render(&surfacelayer->surface,quality,renddesc,&stageone))
97                 return false;
98         // Sets up the interpolation of the context (now the surface layer is the first one)
99         // depending on the quality
100         if(quality<=4)surfacelayer->c=3;else
101         if(quality<=5)surfacelayer->c=2;
102         else if(quality<=6)surfacelayer->c=1;
103         else surfacelayer->c=0;
104         surfacelayer->tl=renddesc.get_tl();
105         surfacelayer->br=renddesc.get_br();
106         // Sets the blend method to straight. See below
107         surfacelayer->set_blend_method(Color::BLEND_STRAIGHT);
108         // Push this layer on the image. The blending result is only this layer
109         // adn the surface layer. The rest of the context is ignored by the straight
110         // blend method of surface layer
111         image.push_front(const_cast<synfig::Layer_Composite*>(this));
112
113         // Set up a surface target
114         Target::Handle target(surface_target(surface));
115
116         if(!target)
117         {
118                 if(cb)cb->error(_("Unable to create surface target"));
119                 return false;
120         }
121
122         RendDesc desc(renddesc);
123
124         target->set_rend_desc(&desc);
125
126         // Render the scene
127         return render(Context(image.begin()),target,desc,&stagetwo);
128         //return render_threaded(Context(image.begin()),target,desc,&stagetwo,2);
129 }
130
131 Rect
132 Layer_Composite::get_full_bounding_rect(Context context)const
133 {
134         if(is_disabled() || Color::is_onto(get_blend_method()))
135                 return context.get_full_bounding_rect();
136
137         return context.get_full_bounding_rect()|get_bounding_rect();
138 }
139
140 Layer::Vocab
141 Layer_Composite::get_param_vocab()const
142 {
143         //! First fills the returning vocabulary with the ancestor class
144         Layer::Vocab ret(Layer::get_param_vocab());
145         //! Now inserts the two parameters that this layer knows.
146         ret.push_back(ParamDesc(amount_,"amount")
147                 .set_local_name(_("Amount"))
148         );
149         ret.push_back(ParamDesc(blend_method_,"blend_method")
150                 .set_local_name(_("Blend Method"))
151         );
152
153         return ret;
154 }
155
156 bool
157 Layer_Composite::set_param(const String & param, const ValueBase &value)
158 {
159         if(param=="amount" && value.same_type_as(amount_))
160                 amount_=value.get(amount_);
161         else
162         if(param=="blend_method" && value.same_type_as(int()))
163         {
164                 blend_method_ = static_cast<Color::BlendMethod>(value.get(int()));
165
166                 if (blend_method_ < 0 || blend_method_ >= Color::BLEND_END)
167                 {
168                         warning("illegal value (%d) for blend_method - using Composite instead", blend_method_);
169                         blend_method_ = Color::BLEND_COMPOSITE;
170                         return false;
171                 }
172
173                 if (blend_method_ == Color::BLEND_STRAIGHT && !reads_context())
174                 {
175                         Canvas::Handle canvas(get_canvas());
176                         if (canvas)
177                         {
178                                 String version(canvas->get_version());
179
180                                 if (version == "0.1" || version == "0.2")
181                                 {
182                                         if (get_name() == "PasteCanvas")
183                                                 warning("loaded a version %s canvas with a 'Straight' blended PasteCanvas (%s) - check it renders OK",
184                                                                 version.c_str(), get_non_empty_description().c_str());
185                                         else
186                                         {
187                                                 blend_method_ = Color::BLEND_COMPOSITE;
188                                                 converted_blend_ = true;
189
190                                                 // if this layer has a transparent color, go back and set the color again
191                                                 // now that we know we are converting the blend method as well.  that will
192                                                 // make the color non-transparent, and change the blend method to alpha over
193                                                 if (transparent_color_)
194                                                         set_param("color", get_param("color"));
195                                         }
196                                 }
197                         }
198                 }
199         }
200         else
201                 return Layer::set_param(param,value);
202         return true;
203 }
204
205 ValueBase
206 Layer_Composite::get_param(const String & param)const
207 {
208         //! First check if the parameter's string is known.
209         if(param=="amount")
210                 return get_amount();
211         if(param=="blend_method")
212                 return static_cast<int>(get_blend_method());
213         //! If it is unknown then call the ancestor's get param member
214         //! to see if it can handle that parameter's string.
215         return Layer::get_param(param);
216 }