moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / layer_composite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file layer_composite.cpp
3 **      \brief Template File
4 **
5 **      $Id: layer_composite.cpp,v 1.2 2005/01/24 03:08:18 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "layer_composite.h"
32 #include "context.h"
33 #include "time.h"
34 #include "color.h"
35 #include "surface.h"
36 #include "renddesc.h"
37 #include "target.h"
38
39 #include "layer_bitmap.h"
40
41 #include "general.h"
42 #include "render.h"
43 #include "paramdesc.h"
44
45 #endif
46
47 /* === U S I N G =========================================================== */
48
49 using namespace std;
50 using namespace etl;
51 using namespace synfig;
52
53 /* === M A C R O S ========================================================= */
54
55 /* === G L O B A L S ======================================================= */
56
57 /* === P R O C E D U R E S ================================================= */
58
59 /* === M E T H O D S ======================================================= */
60
61 bool
62 Layer_Composite::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc_, ProgressCallback *cb)  const
63 {
64         RendDesc renddesc(renddesc_);
65         
66         if(!amount_)
67                 return context.accelerated_render(surface,quality,renddesc,cb);
68
69         CanvasBase image;
70
71         SuperCallback stageone(cb,0,50000,100000);
72         SuperCallback stagetwo(cb,50000,100000,100000);
73
74         Layer_Bitmap::Handle surfacelayer(new class Layer_Bitmap());
75
76         Context iter;
77         
78         for(iter=context;*iter;iter++)
79                 image.push_back(*iter);
80
81         image.push_front(surfacelayer.get());
82
83         // We want to go ahead and schedule any other
84         // layers...
85 //      while(dynamic_cast<Layer_Composite*>(context->get()))
86 //      while(context->get() &&
87 //              &context->get()->AcceleratedRender==
88 //              &Layer_Composite::AcceleratedRender)
89 //              image.push_back(*(context++));
90
91         image.push_back(0);     // Alpha black
92
93         // Render the backdrop
94         if(!context.accelerated_render(&surfacelayer->surface,quality,renddesc,&stageone))
95                 return false;
96
97         if(quality<=4)surfacelayer->c=3;else
98         if(quality<=5)surfacelayer->c=2;
99         else if(quality<=6)surfacelayer->c=1;
100         else surfacelayer->c=0;
101         surfacelayer->tl=renddesc.get_tl();
102         surfacelayer->br=renddesc.get_br();
103         surfacelayer->set_blend_method(Color::BLEND_STRAIGHT);
104
105         image.push_front(const_cast<synfig::Layer_Composite*>(this));
106
107         // Set up a surface target
108         Target::Handle target(surface_target(surface));
109         
110         if(!target)
111         {
112                 if(cb)cb->error(_("Unable to create surface target"));
113                 return false;
114         }
115         
116         RendDesc desc(renddesc);
117         
118         target->set_rend_desc(&desc);
119
120         // Render the scene
121         return render(Context(image.begin()),target,desc,&stagetwo);
122         //return render_threaded(Context(image.begin()),target,desc,&stagetwo,2);
123 }
124
125 Rect
126 Layer_Composite::get_full_bounding_rect(Context context)const
127 {
128         if(is_disabled() || Color::is_onto(get_blend_method()))
129                 return context.get_full_bounding_rect();
130
131         return context.get_full_bounding_rect()|get_bounding_rect();
132 }
133
134 Layer::Vocab
135 Layer_Composite::get_param_vocab()const
136 {
137         Layer::Vocab ret(Layer::get_param_vocab());
138
139         ret.push_back(ParamDesc(amount_,"amount")
140                 .set_local_name(_("Amount"))
141         );
142         ret.push_back(ParamDesc(blend_method_,"blend_method")
143                 .set_local_name(_("Blend Method"))
144         );
145
146         return ret;
147 }
148
149 bool
150 Layer_Composite::set_param(const String & param, const ValueBase &value)
151 {
152         if(param=="amount" && value.same_as(amount_))
153                 amount_=value.get(amount_);
154         else
155         if(param=="blend_method" && value.same_as(int()))
156                 blend_method_=static_cast<Color::BlendMethod>(value.get(int()));
157         else
158                 return Layer::set_param(param,value);
159         return true;
160 }
161
162 ValueBase
163 Layer_Composite::get_param(const String & param)const
164 {
165         if(param=="amount")
166                 return get_amount();
167         if(param=="blend_method")
168                 return static_cast<int>(get_blend_method());
169         return Layer::get_param(param);
170 }