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"
33 #include <glibmm/thread.h>
34 #include <glibmm/dispatcher.h>
40 #ifdef HAVE_SYS_TYPES_H
41 #include <sys/types.h>
44 #ifdef HAVE_SYS_WAIT_H
52 #include <synfig/general.h>
57 /* === U S I N G =========================================================== */
61 using namespace synfig;
62 using namespace studio;
64 #define BOREDOM_TIMEOUT 50
66 #define REJOIN_ON_STOP 1
68 // The Glib::Dispatcher class is broken as of Glibmm 2.4.5.
69 // Defining this macro enables the workaround.
70 #define GLIB_DISPATCHER_BROKEN 1
72 /* === C L A S S E S ======================================================= */
74 class AsyncTarget_Tile : public synfig::Target_Tile
77 etl::handle<synfig::Target_Tile> warm_target;
83 tile_t(const Surface& surface,int x, int y):
89 std::list<tile_t> tile_queue;
92 #ifndef GLIB_DISPATCHER_BROKEN
93 Glib::Dispatcher tile_ready_signal;
95 Glib::Cond cond_tile_queue_empty;
98 sigc::connection ready_connection;
101 AsyncTarget_Tile(etl::handle<synfig::Target_Tile> warm_target):
102 warm_target(warm_target)
104 set_avoid_time_sync(warm_target->get_avoid_time_sync());
105 set_tile_w(warm_target->get_tile_w());
106 set_tile_h(warm_target->get_tile_h());
107 set_canvas(warm_target->get_canvas());
108 set_quality(warm_target->get_quality());
109 set_remove_alpha(warm_target->get_remove_alpha());
110 set_threads(warm_target->get_threads());
111 set_clipping(warm_target->get_clipping());
112 set_rend_desc(&warm_target->rend_desc());
114 #ifndef GLIB_DISPATCHER_BROKEN
115 ready_connection=tile_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready));
121 ready_connection.disconnect();
125 Glib::Mutex::Lock lock(mutex);
129 virtual int total_tiles()const
131 return warm_target->total_tiles();
134 virtual int next_tile(int& x, int& y)
139 return warm_target->next_tile(x,y);
142 virtual int next_frame(Time& time)
146 return warm_target->next_frame(time);
149 virtual bool start_frame(synfig::ProgressCallback *cb=0)
153 return warm_target->start_frame(cb);
156 virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
161 Glib::Mutex::Lock lock(mutex);
162 tile_queue.push_back(tile_t(surface,gx,gy));
163 if(tile_queue.size()==1)
165 #ifdef GLIB_DISPATCHER_BROKEN
166 ready_connection=Glib::signal_timeout().connect(
168 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
183 Glib::Mutex::Lock lock(mutex);
187 cond_tile_queue_empty.signal();
190 while(!tile_queue.empty() && alive_flag)
192 tile_t& tile(tile_queue.front());
194 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
196 tile_queue.pop_front();
198 cond_tile_queue_empty.signal();
201 virtual void end_frame()
205 Glib::Mutex::Lock lock(mutex);
206 if(!tile_queue.empty() && alive_flag)
208 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
214 Glib::Mutex::Lock lock(mutex);
217 return warm_target->end_frame();
223 class AsyncTarget_Scanline : public synfig::Target_Scanline
226 etl::handle<synfig::Target_Scanline> warm_target;
233 #ifndef GLIB_DISPATCHER_BROKEN
234 Glib::Dispatcher frame_ready_signal;
236 Glib::Cond cond_frame_queue_empty;
239 sigc::connection ready_connection;
243 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
244 warm_target(warm_target)
246 set_avoid_time_sync(warm_target->get_avoid_time_sync());
247 set_canvas(warm_target->get_canvas());
248 set_quality(warm_target->get_quality());
249 set_remove_alpha(warm_target->get_remove_alpha());
250 set_threads(warm_target->get_threads());
251 set_rend_desc(&warm_target->rend_desc());
253 #ifndef GLIB_DISPATCHER_BROKEN
254 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
256 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
259 ~AsyncTarget_Scanline()
261 ready_connection.disconnect();
264 virtual int next_frame(Time& time)
268 return warm_target->next_frame(time);
274 Glib::Mutex::Lock lock(mutex);
278 virtual bool start_frame(synfig::ProgressCallback *cb=0)
283 virtual void end_frame()
286 Glib::Mutex::Lock lock(mutex);
292 #ifdef GLIB_DISPATCHER_BROKEN
293 ready_connection=Glib::signal_timeout().connect(
295 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
301 frame_ready_signal();
305 while(alive_flag && !ready_next)
307 Glib::Mutex::Lock lock(mutex);
308 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
314 virtual Color * start_scanline(int scanline)
316 Glib::Mutex::Lock lock(mutex);
318 return surface[scanline];
321 virtual bool end_scanline()
328 Glib::Mutex::Lock lock(mutex);
330 alive_flag=warm_target->add_frame(&surface);
331 cond_frame_queue_empty.signal();
336 /* === G L O B A L S ======================================================= */
338 /* === P R O C E D U R E S ================================================= */
340 /* === M E T H O D S ======================================================= */
342 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
348 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
350 etl::handle<AsyncTarget_Tile> wrap_target(
351 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
354 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
358 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
360 etl::handle<AsyncTarget_Scanline> wrap_target(
361 new AsyncTarget_Scanline(
362 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
366 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
372 AsyncRenderer::~AsyncRenderer()
378 AsyncRenderer::stop()
382 Glib::Mutex::Lock lock(mutex);
383 done_connection.disconnect();
390 render_thread->join();
393 // Make sure all the dispatch crap is cleared out
394 //Glib::MainContext::get_default()->iteration(false);
408 AsyncRenderer::pause()
413 AsyncRenderer::resume()
418 AsyncRenderer::start()
420 done_connection=Glib::signal_timeout().connect(
422 mem_fun(*this,&AsyncRenderer::start_),
430 AsyncRenderer::start_()
432 error=false;success=false;
435 #ifndef GLIB_DISPATCHER_BROKEN
436 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
439 render_thread=Glib::Thread::create(
440 sigc::mem_fun(*this,&AsyncRenderer::render_target),
447 assert(render_thread);
456 AsyncRenderer::render_target()
458 etl::handle<Target> target(AsyncRenderer::target);
460 if(target && target->render())
467 #ifndef REJOIN_ON_STOP
474 #ifdef GLIB_DISPATCHER_BROKEN
475 done_connection=Glib::signal_timeout().connect(
477 mem_fun(*this,&AsyncRenderer::stop),