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;
177 if (!getenv("SYNFIG_DISABLE_OPTIMIZE_LAYER_TREE"))
179 op_canvas = Canvas::create();
180 op_canvas->set_file_name(canvas->get_file_name());
181 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
182 context=op_canvas->get_context();
185 context=canvas->get_context();
187 context=canvas->get_context();
190 // If the quality is set to zero, then we
191 // use the parametric scanline-renderer.
196 if(!synfig::render(context,this,desc,0))
201 if(!synfig::render_threaded(context,this,desc,0,threads_))
205 else // If quality is set otherwise, then we use the accelerated renderer
207 #if USE_PIXELRENDERING_LIMIT
208 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
211 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
212 if (!rowheight) rowheight = 1; // TODO: render partial lines to stay within the limit?
213 int rows = desc.get_h()/rowheight;
214 int lastrowheight = desc.get_h() - rows*rowheight;
218 synfig::info("Render broken up into %d block%s %d pixels tall, and a final block %d pixels tall",
219 rows-1, rows==2?"":"s", rowheight, lastrowheight);
221 // loop through all the full rows
224 throw(string("add_frame(): target panic on start_frame()"));
228 for(int i=0; i < rows; ++i)
230 RendDesc blockrd = desc;
232 //render the strip at the normal size unless it's the last one...
235 if(!lastrowheight) break;
236 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
240 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
243 if(!context.accelerated_render(&surface,quality,blockrd,0))
245 if(cb)cb->error(_("Accelerated Renderer Failure"));
250 int rowspan=sizeof(Color)*surface.get_w();
251 Surface::pen pen = surface.begin();
253 int yoff = i*rowheight;
255 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
257 Color *colordata= start_scanline(y + yoff);
260 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
264 if(get_remove_alpha())
266 for(int i = 0; i < surface.get_w(); i++)
267 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
270 memcpy(colordata,surface[y],rowspan);
274 throw(string("add_frame(): target panic on end_scanline()"));
283 }else //use normal rendering...
288 if(!context.accelerated_render(&surface,quality,desc,0))
290 // For some reason, the accelerated renderer failed.
291 if(cb)cb->error(_("Accelerated Renderer Failure"));
296 // Put the surface we renderer
298 if(!add_frame(&surface))
300 if(cb)cb->error(_("Unable to put surface on target"));
304 #if USE_PIXELRENDERING_LIMIT
308 }while((i=next_frame(t)));
311 // Set the time that we wish to render
312 if(!get_avoid_time_sync() || canvas->get_time()!=t)
316 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
317 Canvas::Handle op_canvas;
318 if (!getenv("SYNFIG_DISABLE_OPTIMIZE_LAYER_TREE"))
320 op_canvas = Canvas::create();
321 op_canvas->set_file_name(canvas->get_file_name());
322 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
323 context=op_canvas->get_context();
326 context=canvas->get_context();
328 context=canvas->get_context();
331 // If the quality is set to zero, then we
332 // use the parametric scanline-renderer.
337 if(!synfig::render(context,this,desc,cb))
342 if(!synfig::render_threaded(context,this,desc,cb,threads_))
346 else // If quality is set otherwise, then we use the accelerated renderer
348 #if USE_PIXELRENDERING_LIMIT
349 if(desc.get_w()*desc.get_h() > PIXEL_RENDERING_LIMIT)
352 int totalheight = desc.get_h();
353 int rowheight = PIXEL_RENDERING_LIMIT/desc.get_w();
354 if (!rowheight) rowheight = 1; // TODO: render partial lines to stay within the limit?
355 int rows = desc.get_h()/rowheight;
356 int lastrowheight = desc.get_h() - rows*rowheight;
360 synfig::info("Render broken up into %d block%s %d pixels tall, and a final block %d pixels tall",
361 rows-1, rows==2?"":"s", rowheight, lastrowheight);
363 // loop through all the full rows
366 throw(string("add_frame(): target panic on start_frame()"));
370 for(int i=0; i < rows; ++i)
372 RendDesc blockrd = desc;
374 //render the strip at the normal size unless it's the last one...
377 if(!lastrowheight) break;
378 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),lastrowheight);
382 blockrd.set_subwindow(0,i*rowheight,desc.get_w(),rowheight);
385 SuperCallback sc(cb, i*rowheight, (i+1)*rowheight, totalheight);
387 if(!context.accelerated_render(&surface,quality,blockrd,&sc))
389 if(cb)cb->error(_("Accelerated Renderer Failure"));
394 int rowspan=sizeof(Color)*surface.get_w();
395 Surface::pen pen = surface.begin();
397 int yoff = i*rowheight;
399 for(y = 0; y < blockrd.get_h(); y++, pen.inc_y())
401 Color *colordata= start_scanline(y + yoff);
404 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
408 if(get_remove_alpha())
410 for(int i = 0; i < surface.get_w(); i++)
411 colordata[i] = Color::blend(surface[y][i],desc.get_bg_color(),1.0f);
414 memcpy(colordata,surface[y],rowspan);
418 throw(string("add_frame(): target panic on end_scanline()"));
424 //I'm done with this part
425 sc.amount_complete(100,100);
435 if(!context.accelerated_render(&surface,quality,desc,cb))
437 if(cb)cb->error(_("Accelerated Renderer Failure"));
442 // Put the surface we renderer
444 if(!add_frame(&surface))
446 if(cb)cb->error(_("Unable to put surface on target"));
450 #if USE_PIXELRENDERING_LIMIT
459 if(cb)cb->error(_("Caught string :")+str);
462 catch(std::bad_alloc)
464 if(cb)cb->error(_("Ran out of memory (Probably a bug)"));
469 if(cb)cb->error(_("Caught unknown error, rethrowing..."));
476 Target_Scanline::add_frame(const Surface *surface)
482 int rowspan=sizeof(Color)*surface->get_w();
483 Surface::const_pen pen=surface->begin();
487 throw(string("add_frame(): target panic on start_frame()"));
491 for(y=0;y<surface->get_h();y++,pen.inc_y())
493 Color *colordata= start_scanline(y);
496 throw(string("add_frame(): call to start_scanline(y) returned NULL"));
500 if(get_remove_alpha())
502 for(int i=0;i<surface->get_w();i++)
503 colordata[i]=Color::blend((*surface)[y][i],desc.get_bg_color(),1.0f);
506 memcpy(colordata,(*surface)[y],rowspan);
510 throw(string("add_frame(): target panic on end_scanline()"));