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
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 "asyncrenderer.h"
34 #include <glibmm/thread.h>
35 #include <glibmm/dispatcher.h>
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
45 #ifdef HAVE_SYS_WAIT_H
53 #include <synfig/general.h>
58 /* === U S I N G =========================================================== */
62 using namespace synfig;
63 using namespace studio;
65 #define BOREDOM_TIMEOUT 50
67 #define REJOIN_ON_STOP 1
69 // The Glib::Dispatcher class is broken as of Glibmm 2.4.5.
70 // Defining this macro enables the workaround.
71 #define GLIB_DISPATCHER_BROKEN 1
73 /* === C L A S S E S ======================================================= */
75 class AsyncTarget_Tile : public synfig::Target_Tile
78 etl::handle<synfig::Target_Tile> warm_target;
84 tile_t(const Surface& surface,int x, int y):
90 std::list<tile_t> tile_queue;
93 #ifndef GLIB_DISPATCHER_BROKEN
94 Glib::Dispatcher tile_ready_signal;
96 Glib::Cond cond_tile_queue_empty;
99 sigc::connection ready_connection;
102 AsyncTarget_Tile(etl::handle<synfig::Target_Tile> warm_target):
103 warm_target(warm_target)
105 set_avoid_time_sync(warm_target->get_avoid_time_sync());
106 set_tile_w(warm_target->get_tile_w());
107 set_tile_h(warm_target->get_tile_h());
108 set_canvas(warm_target->get_canvas());
109 set_quality(warm_target->get_quality());
110 set_remove_alpha(warm_target->get_remove_alpha());
111 set_threads(warm_target->get_threads());
112 set_clipping(warm_target->get_clipping());
113 set_rend_desc(&warm_target->rend_desc());
115 #ifndef GLIB_DISPATCHER_BROKEN
116 ready_connection=tile_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready));
122 ready_connection.disconnect();
126 Glib::Mutex::Lock lock(mutex);
130 virtual int total_tiles()const
132 return warm_target->total_tiles();
135 virtual int next_tile(int& x, int& y)
140 return warm_target->next_tile(x,y);
143 virtual int next_frame(Time& time)
147 return warm_target->next_frame(time);
150 virtual bool start_frame(synfig::ProgressCallback *cb=0)
154 return warm_target->start_frame(cb);
157 virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
162 Glib::Mutex::Lock lock(mutex);
163 tile_queue.push_back(tile_t(surface,gx,gy));
164 if(tile_queue.size()==1)
166 #ifdef GLIB_DISPATCHER_BROKEN
167 ready_connection=Glib::signal_timeout().connect(
169 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
184 Glib::Mutex::Lock lock(mutex);
188 cond_tile_queue_empty.signal();
191 while(!tile_queue.empty() && alive_flag)
193 tile_t& tile(tile_queue.front());
195 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
197 tile_queue.pop_front();
199 cond_tile_queue_empty.signal();
202 virtual void end_frame()
204 if (!single_threaded())
208 Glib::Mutex::Lock lock(mutex);
209 if(!tile_queue.empty() && alive_flag)
211 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
218 Glib::Mutex::Lock lock(mutex);
221 return warm_target->end_frame();
227 class AsyncTarget_Scanline : public synfig::Target_Scanline
230 etl::handle<synfig::Target_Scanline> warm_target;
237 #ifndef GLIB_DISPATCHER_BROKEN
238 Glib::Dispatcher frame_ready_signal;
240 Glib::Cond cond_frame_queue_empty;
243 sigc::connection ready_connection;
247 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
248 warm_target(warm_target)
250 set_avoid_time_sync(warm_target->get_avoid_time_sync());
251 set_canvas(warm_target->get_canvas());
252 set_quality(warm_target->get_quality());
253 set_remove_alpha(warm_target->get_remove_alpha());
254 set_threads(warm_target->get_threads());
255 set_rend_desc(&warm_target->rend_desc());
257 #ifndef GLIB_DISPATCHER_BROKEN
258 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
260 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
263 ~AsyncTarget_Scanline()
265 ready_connection.disconnect();
268 virtual int next_frame(Time& time)
272 return warm_target->next_frame(time);
278 Glib::Mutex::Lock lock(mutex);
282 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
287 virtual void end_frame()
290 Glib::Mutex::Lock lock(mutex);
296 #ifdef GLIB_DISPATCHER_BROKEN
297 ready_connection=Glib::signal_timeout().connect(
299 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
305 frame_ready_signal();
309 if (!single_threaded())
310 while(alive_flag && !ready_next)
312 Glib::Mutex::Lock lock(mutex);
313 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
319 virtual Color * start_scanline(int scanline)
321 Glib::Mutex::Lock lock(mutex);
323 return surface[scanline];
326 virtual bool end_scanline()
333 Glib::Mutex::Lock lock(mutex);
335 alive_flag=warm_target->add_frame(&surface);
336 if (!single_threaded()) cond_frame_queue_empty.signal();
341 /* === G L O B A L S ======================================================= */
343 /* === P R O C E D U R E S ================================================= */
345 /* === M E T H O D S ======================================================= */
347 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
354 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
356 etl::handle<AsyncTarget_Tile> wrap_target(
357 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
360 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
364 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
366 etl::handle<AsyncTarget_Scanline> wrap_target(
367 new AsyncTarget_Scanline(
368 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
372 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
378 AsyncRenderer::~AsyncRenderer()
384 AsyncRenderer::stop()
388 Glib::Mutex::Lock lock(mutex);
389 done_connection.disconnect();
396 if (!single_threaded()) render_thread->join();
399 // Make sure all the dispatch crap is cleared out
400 //Glib::MainContext::get_default()->iteration(false);
414 AsyncRenderer::pause()
419 AsyncRenderer::resume()
424 AsyncRenderer::start()
426 done_connection=Glib::signal_timeout().connect(
428 mem_fun(*this,&AsyncRenderer::start_),
436 AsyncRenderer::rendering_progress()
439 while(studio::App::events_pending()) studio::App::iteration(false);
444 AsyncRenderer::start_()
446 error=false;success=false;
449 #ifndef GLIB_DISPATCHER_BROKEN
450 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
453 if (single_threaded())
455 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
456 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
457 render_thread = (Glib::Thread*)1;
462 render_thread=Glib::Thread::create(
463 sigc::mem_fun(*this,&AsyncRenderer::render_target),
470 assert(render_thread);
480 AsyncRenderer::render_target()
482 etl::handle<Target> target(AsyncRenderer::target);
484 if(target && target->render())
491 #ifndef REJOIN_ON_STOP
498 #ifdef GLIB_DISPATCHER_BROKEN
499 done_connection=Glib::signal_timeout().connect(
501 mem_fun(*this,&AsyncRenderer::stop),