1 /* === S Y N F I G ========================================================= */
2 /*! \file asyncrenderer.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 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 "asyncrenderer.h"
35 #include <glibmm/thread.h>
36 #include <glibmm/dispatcher.h>
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
46 #ifdef HAVE_SYS_WAIT_H
54 #include <synfig/general.h>
61 /* === U S I N G =========================================================== */
65 using namespace synfig;
66 using namespace studio;
68 #define BOREDOM_TIMEOUT 50
70 #define REJOIN_ON_STOP 1
72 // The Glib::Dispatcher class is broken as of Glibmm 2.4.5.
73 // Defining this macro enables the workaround.
74 #define GLIB_DISPATCHER_BROKEN 1
76 /* === C L A S S E S ======================================================= */
78 class AsyncTarget_Tile : public synfig::Target_Tile
81 etl::handle<synfig::Target_Tile> warm_target;
87 tile_t(const Surface& surface,int x, int y):
93 std::list<tile_t> tile_queue;
96 #ifndef GLIB_DISPATCHER_BROKEN
97 Glib::Dispatcher tile_ready_signal;
99 Glib::Cond cond_tile_queue_empty;
102 sigc::connection ready_connection;
105 AsyncTarget_Tile(etl::handle<synfig::Target_Tile> warm_target):
106 warm_target(warm_target)
108 set_avoid_time_sync(warm_target->get_avoid_time_sync());
109 set_tile_w(warm_target->get_tile_w());
110 set_tile_h(warm_target->get_tile_h());
111 set_canvas(warm_target->get_canvas());
112 set_quality(warm_target->get_quality());
113 set_remove_alpha(warm_target->get_remove_alpha());
114 set_threads(warm_target->get_threads());
115 set_clipping(warm_target->get_clipping());
116 set_rend_desc(&warm_target->rend_desc());
118 #ifndef GLIB_DISPATCHER_BROKEN
119 ready_connection=tile_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready));
125 ready_connection.disconnect();
129 Glib::Mutex::Lock lock(mutex);
133 virtual int total_tiles()const
135 return warm_target->total_tiles();
138 virtual int next_tile(int& x, int& y)
143 return warm_target->next_tile(x,y);
146 virtual int next_frame(Time& time)
150 return warm_target->next_frame(time);
153 virtual bool start_frame(synfig::ProgressCallback *cb=0)
157 return warm_target->start_frame(cb);
160 virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
165 Glib::Mutex::Lock lock(mutex);
166 tile_queue.push_back(tile_t(surface,gx,gy));
167 if(tile_queue.size()==1)
169 #ifdef GLIB_DISPATCHER_BROKEN
170 ready_connection=Glib::signal_timeout().connect(
172 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
187 Glib::Mutex::Lock lock(mutex);
191 cond_tile_queue_empty.signal();
194 while(!tile_queue.empty() && alive_flag)
196 tile_t& tile(tile_queue.front());
198 if (getenv("SYNFIG_SHOW_TILE_OUTLINES"))
201 tile.surface.fill(red, 0, 0, 1, tile.surface.get_h());
202 tile.surface.fill(red, 0, 0, tile.surface.get_w(), 1);
205 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
207 tile_queue.pop_front();
209 cond_tile_queue_empty.signal();
212 virtual void end_frame()
214 #ifdef SINGLE_THREADED
215 if (!single_threaded())
220 Glib::Mutex::Lock lock(mutex);
221 if(!tile_queue.empty() && alive_flag)
223 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
229 #ifdef SINGLE_THREADED
232 Glib::Mutex::Lock lock(mutex);
235 return warm_target->end_frame();
241 class AsyncTarget_Scanline : public synfig::Target_Scanline
244 etl::handle<synfig::Target_Scanline> warm_target;
251 #ifndef GLIB_DISPATCHER_BROKEN
252 Glib::Dispatcher frame_ready_signal;
254 Glib::Cond cond_frame_queue_empty;
257 sigc::connection ready_connection;
261 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
262 warm_target(warm_target)
264 set_avoid_time_sync(warm_target->get_avoid_time_sync());
265 set_canvas(warm_target->get_canvas());
266 set_quality(warm_target->get_quality());
267 set_remove_alpha(warm_target->get_remove_alpha());
268 set_threads(warm_target->get_threads());
269 set_rend_desc(&warm_target->rend_desc());
271 #ifndef GLIB_DISPATCHER_BROKEN
272 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
274 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
277 ~AsyncTarget_Scanline()
279 ready_connection.disconnect();
282 virtual int next_frame(Time& time)
286 return warm_target->next_frame(time);
292 Glib::Mutex::Lock lock(mutex);
296 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
301 virtual void end_frame()
304 Glib::Mutex::Lock lock(mutex);
310 #ifdef GLIB_DISPATCHER_BROKEN
311 ready_connection=Glib::signal_timeout().connect(
313 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
319 frame_ready_signal();
323 #ifdef SINGLE_THREADED
324 if (single_threaded())
328 while(alive_flag && !ready_next)
330 Glib::Mutex::Lock lock(mutex);
331 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
337 virtual Color * start_scanline(int scanline)
339 Glib::Mutex::Lock lock(mutex);
341 return surface[scanline];
344 virtual bool end_scanline()
351 Glib::Mutex::Lock lock(mutex);
353 alive_flag=warm_target->add_frame(&surface);
354 #ifdef SINGLE_THREADED
355 if (!single_threaded())
357 cond_frame_queue_empty.signal();
362 /* === G L O B A L S ======================================================= */
364 /* === P R O C E D U R E S ================================================= */
366 /* === M E T H O D S ======================================================= */
368 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
372 #ifdef SINGLE_THREADED
377 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
379 etl::handle<AsyncTarget_Tile> wrap_target(
380 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
383 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
387 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
389 etl::handle<AsyncTarget_Scanline> wrap_target(
390 new AsyncTarget_Scanline(
391 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
395 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
401 AsyncRenderer::~AsyncRenderer()
407 AsyncRenderer::stop()
411 Glib::Mutex::Lock lock(mutex);
412 done_connection.disconnect();
419 #ifdef SINGLE_THREADED
420 if (!single_threaded())
422 render_thread->join();
425 // Make sure all the dispatch crap is cleared out
426 //Glib::MainContext::get_default()->iteration(false);
440 AsyncRenderer::pause()
445 AsyncRenderer::resume()
450 AsyncRenderer::start()
452 done_connection=Glib::signal_timeout().connect(
454 mem_fun(*this,&AsyncRenderer::start_),
461 #ifdef SINGLE_THREADED
463 AsyncRenderer::rendering_progress()
466 while(studio::App::events_pending()) studio::App::iteration(false);
472 AsyncRenderer::start_()
474 error=false;success=false;
477 #ifndef GLIB_DISPATCHER_BROKEN
478 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
481 #ifdef SINGLE_THREADED
482 if (single_threaded())
484 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
485 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
486 render_thread = (Glib::Thread*)1;
492 render_thread=Glib::Thread::create(
493 sigc::mem_fun(*this,&AsyncRenderer::render_target),
500 assert(render_thread);
510 AsyncRenderer::render_target()
512 etl::handle<Target> target(AsyncRenderer::target);
514 if(target && target->render())
521 #ifndef REJOIN_ON_STOP
528 #ifdef GLIB_DISPATCHER_BROKEN
529 done_connection=Glib::signal_timeout().connect(
531 mem_fun(*this,&AsyncRenderer::stop),