70d44309daf5eaf8e9a93c4be45d8720e52a9bbc
[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 Layer_Composite::Layer_Composite(float  a, Color::BlendMethod   bm):
63                 amount                          (a),
64                 blend_method            (bm),
65                 converted_blend_        (false),
66                 transparent_color_      (false)
67         {
68                 Layer::Vocab voc(get_param_vocab());
69                 Layer::fill_static(voc);
70         }
71
72 bool
73 Layer_Composite::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc_, ProgressCallback *cb)  const
74 {
75         RendDesc renddesc(renddesc_);
76
77         if(!amount)
78                 return context.accelerated_render(surface,quality,renddesc,cb);
79
80         CanvasBase image;
81
82         SuperCallback stageone(cb,0,50000,100000);
83         SuperCallback stagetwo(cb,50000,100000,100000);
84
85         Layer_Bitmap::Handle surfacelayer(new class Layer_Bitmap());
86
87         Context iter;
88
89         for(iter=context;*iter;iter++)
90                 image.push_back(*iter);
91
92         image.push_front(surfacelayer.get());
93
94         // We want to go ahead and schedule any other
95         // layers...
96 //      while(dynamic_cast<Layer_Composite*>(context->get()))
97 //      while(context->get() &&
98 //              &context->get()->AcceleratedRender==
99 //              &Layer_Composite::AcceleratedRender)
100 //              image.push_back(*(context++));
101
102         image.push_back(0);     // Alpha black
103
104         // Render the backdrop on the surface layer's surface.
105         if(!context.accelerated_render(&surfacelayer->surface,quality,renddesc,&stageone))
106                 return false;
107         // Sets up the interpolation of the context (now the surface layer is the first one)
108         // depending on the quality
109         if(quality<=4)surfacelayer->c=3;else
110         if(quality<=5)surfacelayer->c=2;
111         else if(quality<=6)surfacelayer->c=1;
112         else surfacelayer->c=0;
113         surfacelayer->tl=renddesc.get_tl();
114         surfacelayer->br=renddesc.get_br();
115         // Sets the blend method to straight. See below
116         surfacelayer->set_blend_method(Color::BLEND_STRAIGHT);
117         // Push this layer on the image. The blending result is only this layer
118         // adn the surface layer. The rest of the context is ignored by the straight
119         // blend method of surface layer
120         image.push_front(const_cast<synfig::Layer_Composite*>(this));
121
122         // Set up a surface target
123         Target::Handle target(surface_target(surface));
124
125         if(!target)
126         {
127                 if(cb)cb->error(_("Unable to create surface target"));
128                 return false;
129         }
130
131         RendDesc desc(renddesc);
132
133         target->set_rend_desc(&desc);
134
135         // Render the scene
136         return render(Context(image.begin()),target,desc,&stagetwo);
137         //return render_threaded(Context(image.begin()),target,desc,&stagetwo,2);
138 }
139
140 Rect
141 Layer_Composite::get_full_bounding_rect(Context context)const
142 {
143         if(is_disabled() || Color::is_onto(get_blend_method()))
144                 return context.get_full_bounding_rect();
145
146         return context.get_full_bounding_rect()|get_bounding_rect();
147 }
148
149 Layer::Vocab
150 Layer_Composite::get_param_vocab()const
151 {
152         //! First fills the returning vocabulary with the ancestor class
153         Layer::Vocab ret(Layer::get_param_vocab());
154         //! Now inserts the two parameters that this layer knows.
155         ret.push_back(ParamDesc(amount,"amount")
156                 .set_local_name(_("Amount"))
157         );
158         ret.push_back(ParamDesc(blend_method,"blend_method")
159                 .set_local_name(_("Blend Method"))
160         );
161
162         return ret;
163 }
164
165 bool
166 Layer_Composite::set_param(const String & param, const ValueBase &value)
167 {
168         if(param=="amount" && value.same_type_as(amount))
169         {
170                 amount=value.get(amount);
171                 set_param_static(param,value.get_static());
172         }
173         else
174         if(param=="blend_method" && value.same_type_as(int()))
175         {
176                 blend_method = static_cast<Color::BlendMethod>(value.get(int()));
177                 set_param_static(param,value.get_static());
178
179                 if (blend_method < 0 || blend_method >= Color::BLEND_END)
180                 {
181                         warning("illegal value (%d) for blend_method - using Composite instead", blend_method);
182                         blend_method = Color::BLEND_COMPOSITE;
183                         return false;
184                 }
185
186                 if (blend_method == Color::BLEND_STRAIGHT && !reads_context())
187                 {
188                         Canvas::Handle canvas(get_canvas());
189                         if (canvas)
190                         {
191                                 String version(canvas->get_version());
192
193                                 if (version == "0.1" || version == "0.2")
194                                 {
195                                         if (get_name() == "PasteCanvas")
196                                                 warning("loaded a version %s canvas with a 'Straight' blended PasteCanvas (%s) - check it renders OK",
197                                                                 version.c_str(), get_non_empty_description().c_str());
198                                         else
199                                         {
200                                                 blend_method = Color::BLEND_COMPOSITE;
201                                                 converted_blend_ = true;
202
203                                                 // if this layer has a transparent color, go back and set the color again
204                                                 // now that we know we are converting the blend method as well.  that will
205                                                 // make the color non-transparent, and change the blend method to alpha over
206                                                 if (transparent_color_)
207                                                         set_param("color", get_param("color"));
208                                         }
209                                 }
210                         }
211                 }
212         }
213         else
214                 return Layer::set_param(param,value);
215         return true;
216 }
217
218 ValueBase
219 Layer_Composite::get_param(const String & param)const
220 {
221
222         //! First check if the parameter's string is known.
223         if(param=="amount")
224         {
225                 synfig::ValueBase ret(get_amount());
226                 ret.set_static(get_param_static(param));
227                 return ret;
228         }
229         if(param=="blend_method")
230         {
231                 synfig::ValueBase ret(static_cast<int>(get_blend_method()));
232                 ret.set_static(get_param_static(param));
233                 return ret;
234         }
235         //! If it is unknown then call the ancestor's get param member
236         //! to see if it can handle that parameter's string.
237         return Layer::get_param(param);
238 }