1 /* === S Y N F I G ========================================================= */
2 /*! \file target_scanline.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "target_scanline.h"
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
47 /* === M A C R O S ========================================================= */
49 #define SYNFIG_OPTIMIZE_LAYER_TREE 1
51 #define PIXEL_RENDERING_LIMIT 1500000
53 #define USE_PIXELRENDERING_LIMIT 1
55 /* === G L O B A L S ======================================================= */
57 /* === P R O C E D U R E S ================================================= */
59 /* === M E T H O D S ======================================================= */
61 Target_Scanline::Target_Scanline():
68 Target_Scanline::next_frame(Time& time)
78 // If the description's end frame is equal to
79 // the start frame, then it is assumed that we
80 // are rendering only one frame. Correct it.
81 if(desc.get_frame_end()==desc.get_frame_start())
82 desc.set_frame_end(desc.get_frame_start()+1);
84 frame_start=desc.get_frame_start();
85 frame_end=desc.get_frame_end();
86 time_start=desc.get_time_start();
87 time_end=desc.get_time_end();
89 // Calculate the number of frames
90 total_frames=frame_end-frame_start;
91 if(total_frames<=0)total_frames=1;
93 //RendDesc rend_desc=desc;
94 //rend_desc.set_gamma(1);
96 // int total_tiles(total_tiles());
97 time=(time_end-time_start)*curr_frame_/total_frames+time_start;
100 /* synfig::info("curr_frame_: %d",curr_frame_);
101 synfig::info("total_frames: %d",total_frames);
102 synfig::info("time_end: %s",time_end.get_string().c_str());
103 synfig::info("time_start: %s",time_start.get_string().c_str());
105 // synfig::info("time: %s",time.get_string().c_str());
107 return total_frames- curr_frame_+1;
110 synfig::Target_Scanline::render(ProgressCallback *cb)
112 SuperCallback super_cb;
116 quality=get_quality(),
128 if(cb) cb->error(_("Target initialisation failure"));
132 // If the description's end frame is equal to
133 // the start frame, then it is assumed that we
134 // are rendering only one frame. Correct it.
135 if(desc.get_frame_end()==desc.get_frame_start())
136 desc.set_frame_end(desc.get_frame_start()+1);
138 frame_start=desc.get_frame_start();
139 frame_end=desc.get_frame_end();
140 time_start=desc.get_time_start();
141 time_end=desc.get_time_end();
143 // Calculate the number of frames
144 total_frames=frame_end-frame_start;
147 //RendDesc rend_desc=desc;
153 //synfig::info("1time_set_to %s",t.get_string().c_str());
159 //for(i=0,t=time_start;i<total_frames;i++)
161 //t=((time_end-time_start)*((Real)i/(Real)total_frames)).round(desc.get_frame_rate())+time_start;
163 // If we have a callback, and it returns
164 // false, go ahead and bail. (it may be a user cancel)
165 if(cb && !cb->amount_complete(total_frames-(i-1),total_frames))
168 // Set the time that we wish to render
169 if(!get_avoid_time_sync() || canvas->get_time()!=t)
174 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
175 Canvas::Handle op_canvas(Canvas::create());
176 optimize_layers(canvas->get_context(), op_canvas);
177 context=op_canvas->get_context();
179 context=canvas->get_context();
182 // If the quality is set to zero, then we
183 // use the parametric scanline-renderer.
188 if(!synfig::render(context,this,desc,0))
193 if(!synfig::render_threaded(context,this,desc,0,threads_))
197 else // If quality is set otherwise, then we use the accelerated renderer
199 #if USE_PIXELRENDERING_LIMIT
200 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
202 synfig::info("Render BROKEN UP! (%d pixels)", desc.get_w()*desc.get_h());
205 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
206 int rows = desc.get_h()/rowheight;
207 int lastrowheight = desc.get_h() - rows*rowheight;
211 synfig::info("\t blockh=%d,remh=%d,totrows=%d", rowheight,lastrowheight,rows);
213 // loop through all the full rows
216 throw(string("add_frame(): target panic on start_frame()"));
220 for(int i=0; i < rows; ++i)
222 RendDesc blockrd = desc;
224 //render the strip at the normal size unless it's the last one...
227 if(!lastrowheight) break;
228 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
232 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
235 if(!context.accelerated_render(&surface,quality,blockrd,0))
237 if(cb)cb->error(_("Accelerated Renderer Failure"));
242 int rowspan=sizeof(Color)*surface.get_w();
243 Surface::pen pen = surface.begin();
245 int yoff = i*rowheight;
247 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
249 Color *colordata= start_scanline(y + yoff);
252 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
256 if(get_remove_alpha())
258 for(int i = 0; i < surface.get_w(); i++)
259 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
262 memcpy(colordata,surface[y],rowspan);
266 throw(string("add_frame(): target panic on end_scanline()"));
275 }else //use normal rendering...
280 if(!context.accelerated_render(&surface,quality,desc,0))
282 // For some reason, the accelerated renderer failed.
283 if(cb)cb->error(_("Accelerated Renderer Failure"));
288 // Put the surface we renderer
290 if(!add_frame(&surface))
292 if(cb)cb->error(_("Unable to put surface on target"));
296 #if USE_PIXELRENDERING_LIMIT
300 }while((i=next_frame(t)));
303 // Set the time that we wish to render
304 if(!get_avoid_time_sync() || canvas->get_time()!=t)
308 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
309 Canvas::Handle op_canvas(Canvas::create());
310 optimize_layers(canvas->get_context(), op_canvas);
311 context=op_canvas->get_context();
313 context=canvas->get_context();
316 // If the quality is set to zero, then we
317 // use the parametric scanline-renderer.
322 if(!synfig::render(context,this,desc,cb))
327 if(!synfig::render_threaded(context,this,desc,cb,threads_))
331 else // If quality is set otherwise, then we use the accelerated renderer
333 #if USE_PIXELRENDERING_LIMIT
334 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
336 synfig::info("Render BROKEN UP! (%d pixels)", desc.get_w()*desc.get_h());
339 int totalheight = desc.get_h();
340 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
341 int rows = desc.get_h()/rowheight;
342 int lastrowheight = desc.get_h() - rows*rowheight;
346 synfig::info("\t blockh=%d,remh=%d,totrows=%d", rowheight,lastrowheight,rows);
348 // loop through all the full rows
351 throw(string("add_frame(): target panic on start_frame()"));
355 for(int i=0; i < rows; ++i)
357 RendDesc blockrd = desc;
359 //render the strip at the normal size unless it's the last one...
362 if(!lastrowheight) break;
363 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
367 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
370 SuperCallback sc(cb, i*rowheight, (i+1)*rowheight, totalheight);
372 if(!context.accelerated_render(&surface,quality,blockrd,&sc))
374 if(cb)cb->error(_("Accelerated Renderer Failure"));
379 int rowspan=sizeof(Color)*surface.get_w();
380 Surface::pen pen = surface.begin();
382 int yoff = i*rowheight;
384 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
386 Color *colordata= start_scanline(y + yoff);
389 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
393 if(get_remove_alpha())
395 for(int i = 0; i < surface.get_w(); i++)
396 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
399 memcpy(colordata,surface[y],rowspan);
403 throw(string("add_frame(): target panic on end_scanline()"));
409 //I'm done with this part
410 sc.amount_complete(100,100);
420 if(!context.accelerated_render(&surface,quality,desc,cb))
422 if(cb)cb->error(_("Accelerated Renderer Failure"));
427 // Put the surface we renderer
429 if(!add_frame(&surface))
431 if(cb)cb->error(_("Unable to put surface on target"));
435 #if USE_PIXELRENDERING_LIMIT
444 if(cb)cb->error(_("Caught string :")+str);
447 catch(std::bad_alloc)
449 if(cb)cb->error(_("Ran out of memory (Probably a bug)"));
454 if(cb)cb->error(_("Caught unknown error, rethrowing..."));
461 Target_Scanline::add_frame(const Surface *surface)
467 int rowspan=sizeof(Color)*surface->get_w();
468 Surface::const_pen pen=surface->begin();
472 throw(string("add_frame(): target panic on start_frame()"));
476 for(y=0;y<surface->get_h();y++,pen.inc_y())
478 Color *colordata= start_scanline(y);
481 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
485 if(get_remove_alpha())
487 for(int i=0;i<surface->get_w();i++)
488 colordata[i]=Color::blend((*surface)[y][i],desc.get_bg_color(),1.0f);
491 memcpy(colordata,(*surface)[y],rowspan);
495 throw(string("add_frame(): target panic on end_scanline()"));