1 /* === S Y N F I G ========================================================= */
2 /*! \file target_tile.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 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_tile.h"
44 /* === U S I N G =========================================================== */
48 using namespace synfig;
50 /* === M A C R O S ========================================================= */
51 const unsigned int DEF_TILE_WIDTH = TILE_SIZE / 2;
52 const unsigned int DEF_TILE_HEIGHT= TILE_SIZE / 2;
54 #define SYNFIG_OPTIMIZE_LAYER_TREE 1
56 // #define SYNFIG_DISPLAY_EFFICIENCY 1
59 /* === G L O B A L S ======================================================= */
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 Target_Tile::Target_Tile():
67 tile_w_(DEF_TILE_WIDTH),
68 tile_h_(DEF_TILE_HEIGHT),
76 Target_Tile::next_frame(Time& time)
86 // If the description's end frame is equal to
87 // the start frame, then it is assumed that we
88 // are rendering only one frame. Correct it.
89 if(desc.get_frame_end()==desc.get_frame_start())
90 desc.set_frame_end(desc.get_frame_start()+1);
92 frame_start=desc.get_frame_start();
93 frame_end=desc.get_frame_end();
94 time_start=desc.get_time_start();
95 time_end=desc.get_time_end();
97 // Calculate the number of frames
98 total_frames=frame_end-frame_start;
99 if(total_frames<=0)total_frames=1;
101 //RendDesc rend_desc=desc;
102 //rend_desc.set_gamma(1);
104 // int total_tiles(total_tiles());
105 time=(time_end-time_start)*curr_frame_/total_frames+time_start;
108 /* synfig::info("curr_frame_: %d",curr_frame_);
109 synfig::info("total_frames: %d",total_frames);
110 synfig::info("time_end: %s",time_end.get_string().c_str());
111 synfig::info("time_start: %s",time_start.get_string().c_str());
113 // synfig::info("time: %s",time.get_string().c_str());
115 return total_frames- curr_frame_+1;
119 Target_Tile::next_tile(int& x, int& y)
121 // Width of the image(in tiles)
122 int tw(rend_desc().get_w()/tile_w_);
123 int th(rend_desc().get_h()/tile_h_);
125 // Add the last tiles (which will be clipped)
126 if(rend_desc().get_w()%tile_w_!=0)tw++;
127 if(rend_desc().get_h()%tile_h_!=0)th++;
129 x=(curr_tile_%tw)*tile_h_;
130 y=(curr_tile_/tw)*tile_w_;
133 return (tw*th)-curr_tile_+1;
137 synfig::Target_Tile::render_frame_(Context context,ProgressCallback *cb)
139 if(tile_w_<=0||tile_h_<=0)
141 if(cb)cb->error(_("Bad Tile Size"));
144 const RendDesc &rend_desc(desc);
145 #define total_tiles total_tiles()
147 etl::clock total_time;
148 etl::clock::value_type work_time(0);
149 etl::clock::value_type find_tile_time(0);
150 etl::clock::value_type add_tile_time(0);
153 // If the quality is set to zero, then we
154 // use the parametric scanline-renderer.
162 etl::clock tile_timer;
164 while((i=next_tile(x,y)))
166 find_tile_time+=tile_timer();
167 SuperCallback super(cb,(total_tiles-i+1)*1000,(total_tiles-i+2)*1000,total_tiles*1000);
168 if(!super.amount_complete(0,1000))
170 //if(cb && !cb->amount_complete(total_tiles-i,total_tiles))
173 // Perform clipping on the tile
176 w=x+tile_w_<rend_desc.get_w()?tile_w_:rend_desc.get_w()-x;
177 h=y+tile_h_<rend_desc.get_h()?tile_h_:rend_desc.get_h()-y;
178 if(w<=0||h<=0)continue;
187 tile_desc.set_subwindow(x,y,w,h);
188 if(!parametric_render(context, surface, tile_desc,&super))
190 // For some reason, the parametric renderer failed.
191 if(cb)cb->error(_("Parametric Renderer Failure"));
198 if(cb)cb->error(_("Bad surface"));
201 if(get_remove_alpha())
202 for(int i=0;i<surface.get_w()*surface.get_h();i++)
203 surface[0][i]=Color::blend(surface[0][i],desc.get_bg_color(),1.0f);
205 // Add the tile to the target
206 if(!add_tile(surface,x,y))
208 if(cb)cb->error(_("add_tile():Unable to put surface on target"));
215 else // If quality is set otherwise, then we use the accelerated renderer
222 etl::clock tile_timer;
224 while((i=next_tile(x,y)))
226 find_tile_time+=tile_timer();
227 SuperCallback super(cb,(total_tiles-i)*1000,(total_tiles-i+1)*1000,total_tiles*1000);
228 if(!super.amount_complete(0,1000))
230 // if(cb && !cb->amount_complete(total_tiles-i,total_tiles))
232 // Perform clipping on the tile
235 w=x+tile_w_<rend_desc.get_w()?tile_w_:rend_desc.get_w()-x;
236 h=y+tile_h_<rend_desc.get_h()?tile_h_:rend_desc.get_h()-y;
237 if(w<=0||h<=0)continue;
246 tile_desc.set_subwindow(x,y,w,h);
251 if(!context.accelerated_render(&surface,get_quality(),tile_desc,&super))
253 // For some reason, the accelerated renderer failed.
254 if(cb)cb->error(_("Accelerated Renderer Failure"));
262 if(cb)cb->error(_("Bad surface"));
265 if(get_remove_alpha())
266 for(int i=0;i<surface.get_w()*surface.get_h();i++)
267 surface[0][i]=Color::blend(surface[0][i],desc.get_bg_color(),1.0f);
271 // Add the tile to the target
272 if(!add_tile(surface,x,y))
274 if(cb)cb->error(_("add_tile():Unable to put surface on target"));
277 add_tile_time+=timer();
283 if(cb && !cb->amount_complete(total_tiles,total_tiles))
286 #if SYNFIG_DISPLAY_EFFICIENCY==1
287 synfig::info(">>>>>> Render Time: %fsec, Find Tile Time: %fsec, Add Tile Time: %fsec, Total Time: %fsec",work_time,find_tile_time,add_tile_time,total_time());
288 synfig::info(">>>>>> FRAME EFFICIENCY: %f%%",(100.0f*work_time/total_time()));
295 synfig::Target_Tile::render(ProgressCallback *cb)
297 SuperCallback super_cb;
312 if(cb) cb->error(_("Target initialization failure"));
317 // If the description's end frame is equal to
318 // the start frame, then it is assumed that we
319 // are rendering only one frame. Correct it.
320 if(desc.get_frame_end()==desc.get_frame_start())
321 desc.set_frame_end(desc.get_frame_start()+1);
323 frame_start=desc.get_frame_start();
324 frame_end=desc.get_frame_end();
325 time_start=desc.get_time_start();
326 time_end=desc.get_time_end();
328 // Calculate the number of frames
329 total_frames=frame_end-frame_start;
337 //synfig::info("1time_set_to %s",t.get_string().c_str());
345 // If we have a callback, and it returns
346 // false, go ahead and bail. (maybe a use cancel)
347 if(cb && !cb->amount_complete(total_frames-(i-1),total_frames))
353 // Set the time that we wish to render
354 //if(!get_avoid_time_sync() || canvas->get_time()!=t)
359 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
360 Canvas::Handle op_canvas(Canvas::create());
361 op_canvas->set_file_name(canvas->get_file_name());
362 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
363 context=op_canvas->get_context();
365 context=canvas->get_context();
369 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
371 Canvas::Handle op_canvas(Canvas::create());
372 op_canvas->set_file_name(canvas->get_file_name());
373 // Set the time that we wish to render
375 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
376 context=op_canvas->get_context();
379 // Set the time that we wish to render
381 context=canvas->get_context();
385 if(!render_frame_(context,0))
388 }while((i=next_frame(t)));
389 //synfig::info("tilerenderer: i=%d, t=%s",i,t.get_string().c_str());
398 // Set the time that we wish to render
399 // if(!get_avoid_time_sync() || canvas->get_time()!=t)
402 //synfig::info("2time_set_to %s",t.get_string().c_str());
406 #ifdef SYNFIG_OPTIMIZE_LAYER_TREE
407 Canvas::Handle op_canvas(Canvas::create());
408 op_canvas->set_file_name(canvas->get_file_name());
409 optimize_layers(canvas->get_time(), canvas->get_context(), op_canvas);
410 context=op_canvas->get_context();
412 context=canvas->get_context();
415 if(!render_frame_(context, cb))
423 if(cb)cb->error(_("Caught string :")+str);
426 catch(std::bad_alloc)
428 if(cb)cb->error(_("Ran out of memory (Probably a bug)"));
433 if(cb)cb->error(_("Caught unknown error, rethrowing..."));