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 initialization 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 op_canvas->set_file_name(canvas->get_file_name());
177 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
178 context=op_canvas->get_context();
180 context=canvas->get_context();
183 // If the quality is set to zero, then we
184 // use the parametric scanline-renderer.
189 if(!synfig::render(context,this,desc,0))
194 if(!synfig::render_threaded(context,this,desc,0,threads_))
198 else // If quality is set otherwise, then we use the accelerated renderer
200 #if USE_PIXELRENDERING_LIMIT
201 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
204 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
205 if (!rowheight) rowheight = 1; // TODO: render partial lines to stay within the limit?
206 int rows = desc.get_h()/rowheight;
207 int lastrowheight = desc.get_h() - rows*rowheight;
211 synfig::info("Render broken up into %d block%s %d pixels tall, and a final block %d pixels tall",
212 rows-1, rows==2?"":"s", rowheight, lastrowheight);
214 // loop through all the full rows
217 throw(string("add_frame(): target panic on start_frame()"));
221 for(int i=0; i < rows; ++i)
223 RendDesc blockrd = desc;
225 //render the strip at the normal size unless it's the last one...
228 if(!lastrowheight) break;
229 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
233 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
236 if(!context.accelerated_render(&surface,quality,blockrd,0))
238 if(cb)cb->error(_("Accelerated Renderer Failure"));
243 int rowspan=sizeof(Color)*surface.get_w();
244 Surface::pen pen = surface.begin();
246 int yoff = i*rowheight;
248 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
250 Color *colordata= start_scanline(y + yoff);
253 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
257 if(get_remove_alpha())
259 for(int i = 0; i < surface.get_w(); i++)
260 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
263 memcpy(colordata,surface[y],rowspan);
267 throw(string("add_frame(): target panic on end_scanline()"));
276 }else //use normal rendering...
281 if(!context.accelerated_render(&surface,quality,desc,0))
283 // For some reason, the accelerated renderer failed.
284 if(cb)cb->error(_("Accelerated Renderer Failure"));
289 // Put the surface we renderer
291 if(!add_frame(&surface))
293 if(cb)cb->error(_("Unable to put surface on target"));
297 #if USE_PIXELRENDERING_LIMIT
301 }while((i=next_frame(t)));
304 // Set the time that we wish to render
305 if(!get_avoid_time_sync() || canvas->get_time()!=t)
309 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
310 Canvas::Handle op_canvas(Canvas::create());
311 op_canvas->set_file_name(canvas->get_file_name());
312 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
313 context=op_canvas->get_context();
315 context=canvas->get_context();
318 // If the quality is set to zero, then we
319 // use the parametric scanline-renderer.
324 if(!synfig::render(context,this,desc,cb))
329 if(!synfig::render_threaded(context,this,desc,cb,threads_))
333 else // If quality is set otherwise, then we use the accelerated renderer
335 #if USE_PIXELRENDERING_LIMIT
336 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
339 int totalheight = desc.get_h();
340 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
341 if (!rowheight) rowheight = 1; // TODO: render partial lines to stay within the limit?
342 int rows = desc.get_h()/rowheight;
343 int lastrowheight = desc.get_h() - rows*rowheight;
347 synfig::info("Render broken up into %d block%s %d pixels tall, and a final block %d pixels tall",
348 rows-1, rows==2?"":"s", rowheight, lastrowheight);
350 // loop through all the full rows
353 throw(string("add_frame(): target panic on start_frame()"));
357 for(int i=0; i < rows; ++i)
359 RendDesc blockrd = desc;
361 //render the strip at the normal size unless it's the last one...
364 if(!lastrowheight) break;
365 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
369 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
372 SuperCallback sc(cb, i*rowheight, (i+1)*rowheight, totalheight);
374 if(!context.accelerated_render(&surface,quality,blockrd,&sc))
376 if(cb)cb->error(_("Accelerated Renderer Failure"));
381 int rowspan=sizeof(Color)*surface.get_w();
382 Surface::pen pen = surface.begin();
384 int yoff = i*rowheight;
386 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
388 Color *colordata= start_scanline(y + yoff);
391 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
395 if(get_remove_alpha())
397 for(int i = 0; i < surface.get_w(); i++)
398 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
401 memcpy(colordata,surface[y],rowspan);
405 throw(string("add_frame(): target panic on end_scanline()"));
411 //I'm done with this part
412 sc.amount_complete(100,100);
422 if(!context.accelerated_render(&surface,quality,desc,cb))
424 if(cb)cb->error(_("Accelerated Renderer Failure"));
429 // Put the surface we renderer
431 if(!add_frame(&surface))
433 if(cb)cb->error(_("Unable to put surface on target"));
437 #if USE_PIXELRENDERING_LIMIT
446 if(cb)cb->error(_("Caught string :")+str);
449 catch(std::bad_alloc)
451 if(cb)cb->error(_("Ran out of memory (Probably a bug)"));
456 if(cb)cb->error(_("Caught unknown error, rethrowing..."));
463 Target_Scanline::add_frame(const Surface *surface)
469 int rowspan=sizeof(Color)*surface->get_w();
470 Surface::const_pen pen=surface->begin();
474 throw(string("add_frame(): target panic on start_frame()"));
478 for(y=0;y<surface->get_h();y++,pen.inc_y())
480 Color *colordata= start_scanline(y);
483 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
487 if(get_remove_alpha())
489 for(int i=0;i<surface->get_w();i++)
490 colordata[i]=Color::blend((*surface)[y][i],desc.get_bg_color(),1.0f);
493 memcpy(colordata,(*surface)[y],rowspan);
497 throw(string("add_frame(): target panic on end_scanline()"));