Author: Carlos Lopez <[email protected]>
Date:   Fri Sep 28 20:25:13 2012 +0200

Bevel layer: Add support for Cairo render

---

 synfig-core/src/modules/lyr_std/bevel.cpp |  210 +++++++++++++++++++++++++++++
 synfig-core/src/modules/lyr_std/bevel.h   |    1 +
 2 files changed, 211 insertions(+), 0 deletions(-)

diff --git a/synfig-core/src/modules/lyr_std/bevel.cpp 
b/synfig-core/src/modules/lyr_std/bevel.cpp
index 7821833..060b8d2 100644
--- a/synfig-core/src/modules/lyr_std/bevel.cpp
+++ b/synfig-core/src/modules/lyr_std/bevel.cpp
@@ -349,6 +349,216 @@ Layer_Bevel::accelerated_render(Context context,Surface 
*surface,int quality, co
        return true;
 }
 
+////
+bool
+Layer_Bevel::accelerated_cairorender(Context context,cairo_surface_t 
*surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
+{
+       int x,y;
+       SuperCallback stageone(cb,0,5000,10000);
+       SuperCallback stagetwo(cb,5000,10000,10000);
+       
+       const int       w = renddesc.get_w(),
+       h = renddesc.get_h();
+       const Real      pw = renddesc.get_pw(),
+       ph = renddesc.get_ph();
+       const Vector size(softness,softness);
+       
+       RendDesc        workdesc(renddesc);
+       cairo_surface_t         *worksurface;
+       etl::surface<float> blurred;
+       
+       //callbacks depend on how long the blur takes
+       if(size[0] || size[1])
+       {
+               if(type == Blur::DISC)
+               {
+                       stageone = SuperCallback(cb,0,5000,10000);
+                       stagetwo = SuperCallback(cb,5000,10000,10000);
+               }
+               else
+               {
+                       stageone = SuperCallback(cb,0,9000,10000);
+                       stagetwo = SuperCallback(cb,9000,10000,10000);
+               }
+       }
+       else
+       {
+               stageone = SuperCallback(cb,0,9999,10000);
+               stagetwo = SuperCallback(cb,9999,10000,10000);
+       }
+       
+       //expand the working surface to accommodate the blur
+       
+       //the expanded size = 1/2 the size in each direction rounded up
+       int     halfsizex = (int) (abs(size[0]*.5/pw) + 3),
+       halfsizey = (int) (abs(size[1]*.5/ph) + 3);
+       
+       int 
offset_u(round_to_int(offset[0]/pw)),offset_v(round_to_int(offset[1]/ph));
+       int offset_w(w+abs(offset_u)*2),offset_h(h+abs(offset_v)*2);
+       
+       workdesc.set_subwindow(
+                                                  -abs(offset_u),
+                                                  -abs(offset_v),
+                                                  w+abs(offset_u),
+                                                  h+abs(offset_v)
+                                                  );
+       
+       //expand by 1/2 size in each direction on either side
+       switch(type)
+       {
+               case Blur::DISC:
+               case Blur::BOX:
+               case Blur::CROSS:
+               {
+                       
workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
+                       break;
+               }
+               case Blur::FASTGAUSSIAN:
+               {
+                       if(quality < 4)
+                       {
+                               halfsizex*=2;
+                               halfsizey*=2;
+                       }
+                       
workdesc.set_subwindow(-max(1,halfsizex),-max(1,halfsizey),offset_w+2*max(1,halfsizex),offset_h+2*max(1,halfsizey));
+                       break;
+               }
+               case Blur::GAUSSIAN:
+               {
+#define GAUSSIAN_ADJUSTMENT            (0.05)
+                       Real    pw = 
(Real)workdesc.get_w()/(workdesc.get_br()[0]-workdesc.get_tl()[0]);
+                       Real    ph = 
(Real)workdesc.get_h()/(workdesc.get_br()[1]-workdesc.get_tl()[1]);
+                       
+                       pw=pw*pw;
+                       ph=ph*ph;
+                       
+                       halfsizex = 
(int)(abs(pw)*size[0]*GAUSSIAN_ADJUSTMENT+0.5);
+                       halfsizey = 
(int)(abs(ph)*size[1]*GAUSSIAN_ADJUSTMENT+0.5);
+                       
+                       halfsizex = (halfsizex + 1)/2;
+                       halfsizey = (halfsizey + 1)/2;
+                       workdesc.set_subwindow( -halfsizex, -halfsizey, 
offset_w+2*halfsizex, offset_h+2*halfsizey );
+                       
+                       break;
+               }
+       }
+       
+       // setup the worksurface
+       worksurface=cairo_surface_create_similar(surface, 
CAIRO_CONTENT_COLOR_ALPHA, workdesc.get_w(), workdesc.get_h());
+       //render the background onto the expanded surface
+       
if(!context.accelerated_cairorender(worksurface,quality,workdesc,&stageone))
+               return false;
+       
+       // Extract the CairoSurface from the cairo_surface_t
+       CairoSurface cairoworksurface(worksurface);
+       if(!cairoworksurface.map_cairo_image())
+       {
+               synfig::info("map cairo image failed");
+               return false;
+       }
+       // Extract the CairoSurface from the cairo_surface_t
+       CairoSurface cairosurface(surface);
+       if(!cairosurface.map_cairo_image())
+       {
+               synfig::info("map cairo image failed");
+               return false;
+       }
+
+       // Copy over the alpha
+       blurred.set_wh(workdesc.get_w(),workdesc.get_h());
+       if(!use_luma)
+       {
+               for(int j=0;j<workdesc.get_h();j++)
+                       for(int i=0;i<workdesc.get_w();i++)
+                       {
+                               
blurred[j][i]=cairoworksurface[j][i].get_a()/CairoColor::ceil;
+                       }
+       }
+       else
+       {
+               for(int j=0;j<workdesc.get_h();j++)
+                       for(int i=0;i<workdesc.get_w();i++)
+                       {
+                               
blurred[j][i]=cairoworksurface[j][i].get_a()*(cairoworksurface[j][i].get_y()/CairoColor::ceil);
+                       }
+       }
+       
+       //blur the image
+       
Blur(size,type,&stagetwo)(blurred,workdesc.get_br()-workdesc.get_tl(),blurred);
+               
+       int u = halfsizex+abs(offset_u), v = halfsizey+abs(offset_v);
+       for(y=0;y<renddesc.get_h();y++,v++)
+       {
+               u = halfsizex+abs(offset_u);
+               for(x=0;x<renddesc.get_w();x++,u++)
+               {
+                       Real alpha(0);
+                       Color shade;
+                       
+                       {
+                               const float u2(offset[0]/pw),v2(offset[1]/ph);
+                               alpha+=1.0f-blurred.linear_sample(u2+u,v2+v);
+                       }
+                       {
+                               const float u2(-offset[0]/pw),v2(-offset[1]/ph);
+                               alpha-=1.0f-blurred.linear_sample(u2+u,v2+v);
+                       }
+                       {
+                               const float 
u2(offset45[0]/pw),v2(offset45[1]/ph);
+                               
alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
+                       }
+                       {
+                               const float 
u2(offset45[1]/ph),v2(-offset45[0]/pw);
+                               
alpha+=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
+                       }
+                       {
+                               const float 
u2(-offset45[0]/pw),v2(-offset45[1]/ph);
+                               
alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
+                       }
+                       {
+                               const float 
u2(-offset45[1]/ph),v2(offset45[0]/pw);
+                               
alpha-=1.0f-blurred.linear_sample(u2+u,v2+v)*0.5f;
+                       }
+                       
+                       if(solid)
+                       {
+                               alpha/=4.0f;
+                               alpha+=0.5f;
+                               
shade=Color::blend(color1,color2,alpha,Color::BLEND_STRAIGHT);
+                       }
+                       else
+                       {
+                               alpha/=2;
+                               if(alpha>0)
+                                       
shade=color1,shade.set_a(shade.get_a()*alpha);
+                               else
+                                       
shade=color2,shade.set_a(shade.get_a()*-alpha);
+                       }
+                       
+                       
+                       
+                       if(shade.get_a())
+                       {
+                               
cairosurface[y][x]=CairoColor(Color::blend(shade,Color(cairoworksurface[v][u].demult_alpha()),get_amount(),get_blend_method())).premult_alpha();
+                       }
+                       else cairosurface[y][x] = cairoworksurface[v][u];
+               }
+       }
+       cairoworksurface.unmap_cairo_image();
+       cairosurface.unmap_cairo_image();
+       cairo_surface_destroy(worksurface);
+       
+       if(cb && !cb->amount_complete(10000,10000))
+       {
+               //if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer 
Failure",__LINE__));
+               return false;
+       }
+       
+       return true;
+}
+
+////
+
 Layer::Vocab
 Layer_Bevel::get_param_vocab(void)const
 {
diff --git a/synfig-core/src/modules/lyr_std/bevel.h 
b/synfig-core/src/modules/lyr_std/bevel.h
index 0774b75..ee04583 100644
--- a/synfig-core/src/modules/lyr_std/bevel.h
+++ b/synfig-core/src/modules/lyr_std/bevel.h
@@ -68,6 +68,7 @@ public:
        virtual Color get_color(Context context, const Point &pos)const;
 
        virtual bool accelerated_render(Context context,Surface *surface,int 
quality, const RendDesc &renddesc, ProgressCallback *cb)const;
+       virtual bool accelerated_cairorender(Context context,cairo_surface_t 
*surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const;
 
        virtual synfig::Rect get_full_bounding_rect(Context context)const;
        virtual Vocab get_param_vocab()const;


------------------------------------------------------------------------------
Got visibility?
Most devs has no idea what their production app looks like.
Find out how fast your code is with AppDynamics Lite.
http://ad.doubleclick.net/clk;262219671;13503038;y?
http://info.appdynamics.com/FreeJavaPerformanceDownload.html
_______________________________________________
Synfig-devl mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/synfig-devl

Reply via email to