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
9 ** Copyright (c) 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include "target_scanline.h"
42 /* === U S I N G =========================================================== */
46 using namespace synfig;
48 /* === M A C R O S ========================================================= */
50 #define SYNFIG_OPTIMIZE_LAYER_TREE
52 #define PIXEL_RENDERING_LIMIT 1500000
54 #define USE_PIXELRENDERING_LIMIT 1
56 /* === G L O B A L S ======================================================= */
58 /* === P R O C E D U R E S ================================================= */
60 /* === M E T H O D S ======================================================= */
62 Target_Scanline::Target_Scanline():
69 Target_Scanline::next_frame(Time& time)
79 // If the description's end frame is equal to
80 // the start frame, then it is assumed that we
81 // are rendering only one frame. Correct it.
82 if(desc.get_frame_end()==desc.get_frame_start())
83 desc.set_frame_end(desc.get_frame_start()+1);
85 frame_start=desc.get_frame_start();
86 frame_end=desc.get_frame_end();
87 time_start=desc.get_time_start();
88 time_end=desc.get_time_end();
90 // Calculate the number of frames
91 total_frames=frame_end-frame_start;
92 if(total_frames<=0)total_frames=1;
94 //RendDesc rend_desc=desc;
95 //rend_desc.set_gamma(1);
97 // int total_tiles(total_tiles());
98 time=(time_end-time_start)*curr_frame_/total_frames+time_start;
101 /* synfig::info("curr_frame_: %d",curr_frame_);
102 synfig::info("total_frames: %d",total_frames);
103 synfig::info("time_end: %s",time_end.get_string().c_str());
104 synfig::info("time_start: %s",time_start.get_string().c_str());
106 // synfig::info("time: %s",time.get_string().c_str());
108 return total_frames- curr_frame_+1;
111 synfig::Target_Scanline::render(ProgressCallback *cb)
113 SuperCallback super_cb;
117 quality=get_quality(),
129 if(cb) cb->error(_("Target initialization failure"));
133 // If the description's end frame is equal to
134 // the start frame, then it is assumed that we
135 // are rendering only one frame. Correct it.
136 if(desc.get_frame_end()==desc.get_frame_start())
137 desc.set_frame_end(desc.get_frame_start()+1);
139 frame_start=desc.get_frame_start();
140 frame_end=desc.get_frame_end();
141 time_start=desc.get_time_start();
142 time_end=desc.get_time_end();
144 // Calculate the number of frames
145 total_frames=frame_end-frame_start;
148 //RendDesc rend_desc=desc;
154 //synfig::info("1time_set_to %s",t.get_string().c_str());
160 //for(i=0,t=time_start;i<total_frames;i++)
162 //t=((time_end-time_start)*((Real)i/(Real)total_frames)).round(desc.get_frame_rate())+time_start;
164 // If we have a callback, and it returns
165 // false, go ahead and bail. (it may be a user cancel)
166 if(cb && !cb->amount_complete(total_frames-(i-1),total_frames))
169 // Set the time that we wish to render
170 if(!get_avoid_time_sync() || canvas->get_time()!=t)
175 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
176 Canvas::Handle op_canvas(Canvas::create());
177 op_canvas->set_file_name(canvas->get_file_name());
178 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
179 context=op_canvas->get_context();
181 context=canvas->get_context();
184 // If the quality is set to zero, then we
185 // use the parametric scanline-renderer.
190 if(!synfig::render(context,this,desc,0))
195 if(!synfig::render_threaded(context,this,desc,0,threads_))
199 else // If quality is set otherwise, then we use the accelerated renderer
201 #if USE_PIXELRENDERING_LIMIT
202 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
205 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
206 if (!rowheight) rowheight = 1; // TODO: render partial lines to stay within the limit?
207 int rows = desc.get_h()/rowheight;
208 int lastrowheight = desc.get_h() - rows*rowheight;
212 synfig::info("Render broken up into %d block%s %d pixels tall, and a final block %d pixels tall",
213 rows-1, rows==2?"":"s", rowheight, lastrowheight);
215 // loop through all the full rows
218 throw(string("add_frame(): target panic on start_frame()"));
222 for(int i=0; i < rows; ++i)
224 RendDesc blockrd = desc;
226 //render the strip at the normal size unless it's the last one...
229 if(!lastrowheight) break;
230 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
234 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
237 if(!context.accelerated_render(&surface,quality,blockrd,0))
239 if(cb)cb->error(_("Accelerated Renderer Failure"));
244 int rowspan=sizeof(Color)*surface.get_w();
245 Surface::pen pen = surface.begin();
247 int yoff = i*rowheight;
249 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
251 Color *colordata= start_scanline(y + yoff);
254 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
258 if(get_remove_alpha())
260 for(int i = 0; i < surface.get_w(); i++)
261 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
264 memcpy(colordata,surface[y],rowspan);
268 throw(string("add_frame(): target panic on end_scanline()"));
277 }else //use normal rendering...
282 if(!context.accelerated_render(&surface,quality,desc,0))
284 // For some reason, the accelerated renderer failed.
285 if(cb)cb->error(_("Accelerated Renderer Failure"));
290 // Put the surface we renderer
292 if(!add_frame(&surface))
294 if(cb)cb->error(_("Unable to put surface on target"));
298 #if USE_PIXELRENDERING_LIMIT
302 }while((i=next_frame(t)));
305 // Set the time that we wish to render
306 if(!get_avoid_time_sync() || canvas->get_time()!=t)
310 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
311 Canvas::Handle op_canvas(Canvas::create());
312 op_canvas->set_file_name(canvas->get_file_name());
313 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
314 context=op_canvas->get_context();
316 context=canvas->get_context();
319 // If the quality is set to zero, then we
320 // use the parametric scanline-renderer.
325 if(!synfig::render(context,this,desc,cb))
330 if(!synfig::render_threaded(context,this,desc,cb,threads_))
334 else // If quality is set otherwise, then we use the accelerated renderer
336 #if USE_PIXELRENDERING_LIMIT
337 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
340 int totalheight = desc.get_h();
341 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
342 if (!rowheight) rowheight = 1; // TODO: render partial lines to stay within the limit?
343 int rows = desc.get_h()/rowheight;
344 int lastrowheight = desc.get_h() - rows*rowheight;
348 synfig::info("Render broken up into %d block%s %d pixels tall, and a final block %d pixels tall",
349 rows-1, rows==2?"":"s", rowheight, lastrowheight);
351 // loop through all the full rows
354 throw(string("add_frame(): target panic on start_frame()"));
358 for(int i=0; i < rows; ++i)
360 RendDesc blockrd = desc;
362 //render the strip at the normal size unless it's the last one...
365 if(!lastrowheight) break;
366 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
370 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
373 SuperCallback sc(cb, i*rowheight, (i+1)*rowheight, totalheight);
375 if(!context.accelerated_render(&surface,quality,blockrd,&sc))
377 if(cb)cb->error(_("Accelerated Renderer Failure"));
382 int rowspan=sizeof(Color)*surface.get_w();
383 Surface::pen pen = surface.begin();
385 int yoff = i*rowheight;
387 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
389 Color *colordata= start_scanline(y + yoff);
392 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
396 if(get_remove_alpha())
398 for(int i = 0; i < surface.get_w(); i++)
399 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
402 memcpy(colordata,surface[y],rowspan);
406 throw(string("add_frame(): target panic on end_scanline()"));
412 //I'm done with this part
413 sc.amount_complete(100,100);
423 if(!context.accelerated_render(&surface,quality,desc,cb))
425 if(cb)cb->error(_("Accelerated Renderer Failure"));
430 // Put the surface we renderer
432 if(!add_frame(&surface))
434 if(cb)cb->error(_("Unable to put surface on target"));
438 #if USE_PIXELRENDERING_LIMIT
447 if(cb)cb->error(_("Caught string :")+str);
450 catch(std::bad_alloc)
452 if(cb)cb->error(_("Ran out of memory (Probably a bug)"));
457 if(cb)cb->error(_("Caught unknown error, rethrowing..."));
464 Target_Scanline::add_frame(const Surface *surface)
470 int rowspan=sizeof(Color)*surface->get_w();
471 Surface::const_pen pen=surface->begin();
475 throw(string("add_frame(): target panic on start_frame()"));
479 for(y=0;y<surface->get_h();y++,pen.inc_y())
481 Color *colordata= start_scanline(y);
484 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
488 if(get_remove_alpha())
490 for(int i=0;i<surface->get_w();i++)
491 colordata[i]=Color::blend((*surface)[y][i],desc.get_bg_color(),1.0f);
494 memcpy(colordata,(*surface)[y],rowspan);
498 throw(string("add_frame(): target panic on end_scanline()"));