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>
60 /* === U S I N G =========================================================== */
64 using namespace synfig;
65 using namespace studio;
67 #define BOREDOM_TIMEOUT 50
69 #define REJOIN_ON_STOP 1
71 // The Glib::Dispatcher class is broken as of Glibmm 2.4.5.
72 // Defining this macro enables the workaround.
73 #define GLIB_DISPATCHER_BROKEN 1
75 /* === C L A S S E S ======================================================= */
77 class AsyncTarget_Tile : public synfig::Target_Tile
80 etl::handle<synfig::Target_Tile> warm_target;
86 tile_t(const Surface& surface,int x, int y):
92 std::list<tile_t> tile_queue;
95 #ifndef GLIB_DISPATCHER_BROKEN
96 Glib::Dispatcher tile_ready_signal;
98 Glib::Cond cond_tile_queue_empty;
101 sigc::connection ready_connection;
104 AsyncTarget_Tile(etl::handle<synfig::Target_Tile> warm_target):
105 warm_target(warm_target)
107 set_avoid_time_sync(warm_target->get_avoid_time_sync());
108 set_tile_w(warm_target->get_tile_w());
109 set_tile_h(warm_target->get_tile_h());
110 set_canvas(warm_target->get_canvas());
111 set_quality(warm_target->get_quality());
112 set_remove_alpha(warm_target->get_remove_alpha());
113 set_threads(warm_target->get_threads());
114 set_clipping(warm_target->get_clipping());
115 set_rend_desc(&warm_target->rend_desc());
117 #ifndef GLIB_DISPATCHER_BROKEN
118 ready_connection=tile_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready));
124 ready_connection.disconnect();
128 Glib::Mutex::Lock lock(mutex);
132 virtual int total_tiles()const
134 return warm_target->total_tiles();
137 virtual int next_tile(int& x, int& y)
142 return warm_target->next_tile(x,y);
145 virtual int next_frame(Time& time)
149 return warm_target->next_frame(time);
152 virtual bool start_frame(synfig::ProgressCallback *cb=0)
156 return warm_target->start_frame(cb);
159 virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
164 Glib::Mutex::Lock lock(mutex);
165 tile_queue.push_back(tile_t(surface,gx,gy));
166 if(tile_queue.size()==1)
168 #ifdef GLIB_DISPATCHER_BROKEN
169 ready_connection=Glib::signal_timeout().connect(
171 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
186 Glib::Mutex::Lock lock(mutex);
190 cond_tile_queue_empty.signal();
193 while(!tile_queue.empty() && alive_flag)
195 tile_t& tile(tile_queue.front());
197 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
199 tile_queue.pop_front();
201 cond_tile_queue_empty.signal();
204 virtual void end_frame()
206 if (!single_threaded())
210 Glib::Mutex::Lock lock(mutex);
211 if(!tile_queue.empty() && alive_flag)
213 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
220 Glib::Mutex::Lock lock(mutex);
223 return warm_target->end_frame();
229 class AsyncTarget_Scanline : public synfig::Target_Scanline
232 etl::handle<synfig::Target_Scanline> warm_target;
239 #ifndef GLIB_DISPATCHER_BROKEN
240 Glib::Dispatcher frame_ready_signal;
242 Glib::Cond cond_frame_queue_empty;
245 sigc::connection ready_connection;
249 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
250 warm_target(warm_target)
252 set_avoid_time_sync(warm_target->get_avoid_time_sync());
253 set_canvas(warm_target->get_canvas());
254 set_quality(warm_target->get_quality());
255 set_remove_alpha(warm_target->get_remove_alpha());
256 set_threads(warm_target->get_threads());
257 set_rend_desc(&warm_target->rend_desc());
259 #ifndef GLIB_DISPATCHER_BROKEN
260 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
262 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
265 ~AsyncTarget_Scanline()
267 ready_connection.disconnect();
270 virtual int next_frame(Time& time)
274 return warm_target->next_frame(time);
280 Glib::Mutex::Lock lock(mutex);
284 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
289 virtual void end_frame()
292 Glib::Mutex::Lock lock(mutex);
298 #ifdef GLIB_DISPATCHER_BROKEN
299 ready_connection=Glib::signal_timeout().connect(
301 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
307 frame_ready_signal();
311 if (single_threaded())
314 while(alive_flag && !ready_next)
316 Glib::Mutex::Lock lock(mutex);
317 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
323 virtual Color * start_scanline(int scanline)
325 Glib::Mutex::Lock lock(mutex);
327 return surface[scanline];
330 virtual bool end_scanline()
337 Glib::Mutex::Lock lock(mutex);
339 alive_flag=warm_target->add_frame(&surface);
340 if (!single_threaded()) cond_frame_queue_empty.signal();
345 /* === G L O B A L S ======================================================= */
347 /* === P R O C E D U R E S ================================================= */
349 /* === M E T H O D S ======================================================= */
351 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
358 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
360 etl::handle<AsyncTarget_Tile> wrap_target(
361 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
364 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
368 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
370 etl::handle<AsyncTarget_Scanline> wrap_target(
371 new AsyncTarget_Scanline(
372 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
376 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
382 AsyncRenderer::~AsyncRenderer()
388 AsyncRenderer::stop()
392 Glib::Mutex::Lock lock(mutex);
393 done_connection.disconnect();
400 if (!single_threaded()) render_thread->join();
403 // Make sure all the dispatch crap is cleared out
404 //Glib::MainContext::get_default()->iteration(false);
418 AsyncRenderer::pause()
423 AsyncRenderer::resume()
428 AsyncRenderer::start()
430 done_connection=Glib::signal_timeout().connect(
432 mem_fun(*this,&AsyncRenderer::start_),
440 AsyncRenderer::rendering_progress()
443 while(studio::App::events_pending()) studio::App::iteration(false);
448 AsyncRenderer::start_()
450 error=false;success=false;
453 #ifndef GLIB_DISPATCHER_BROKEN
454 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
457 if (single_threaded())
459 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
460 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
461 render_thread = (Glib::Thread*)1;
466 render_thread=Glib::Thread::create(
467 sigc::mem_fun(*this,&AsyncRenderer::render_target),
474 assert(render_thread);
484 AsyncRenderer::render_target()
486 etl::handle<Target> target(AsyncRenderer::target);
488 if(target && target->render())
495 #ifndef REJOIN_ON_STOP
502 #ifdef GLIB_DISPATCHER_BROKEN
503 done_connection=Glib::signal_timeout().connect(
505 mem_fun(*this,&AsyncRenderer::stop),