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 if (getenv("SYNFIG_SHOW_TILE_OUTLINES"))
200 tile.surface.fill(red, 0, 0, 1, tile.surface.get_h());
201 tile.surface.fill(red, 0, 0, tile.surface.get_w(), 1);
204 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
206 tile_queue.pop_front();
208 cond_tile_queue_empty.signal();
211 virtual void end_frame()
213 if (!single_threaded())
217 Glib::Mutex::Lock lock(mutex);
218 if(!tile_queue.empty() && alive_flag)
220 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
227 Glib::Mutex::Lock lock(mutex);
230 return warm_target->end_frame();
236 class AsyncTarget_Scanline : public synfig::Target_Scanline
239 etl::handle<synfig::Target_Scanline> warm_target;
246 #ifndef GLIB_DISPATCHER_BROKEN
247 Glib::Dispatcher frame_ready_signal;
249 Glib::Cond cond_frame_queue_empty;
252 sigc::connection ready_connection;
256 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
257 warm_target(warm_target)
259 set_avoid_time_sync(warm_target->get_avoid_time_sync());
260 set_canvas(warm_target->get_canvas());
261 set_quality(warm_target->get_quality());
262 set_remove_alpha(warm_target->get_remove_alpha());
263 set_threads(warm_target->get_threads());
264 set_rend_desc(&warm_target->rend_desc());
266 #ifndef GLIB_DISPATCHER_BROKEN
267 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
269 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
272 ~AsyncTarget_Scanline()
274 ready_connection.disconnect();
277 virtual int next_frame(Time& time)
281 return warm_target->next_frame(time);
287 Glib::Mutex::Lock lock(mutex);
291 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
296 virtual void end_frame()
299 Glib::Mutex::Lock lock(mutex);
305 #ifdef GLIB_DISPATCHER_BROKEN
306 ready_connection=Glib::signal_timeout().connect(
308 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
314 frame_ready_signal();
318 if (single_threaded())
321 while(alive_flag && !ready_next)
323 Glib::Mutex::Lock lock(mutex);
324 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
330 virtual Color * start_scanline(int scanline)
332 Glib::Mutex::Lock lock(mutex);
334 return surface[scanline];
337 virtual bool end_scanline()
344 Glib::Mutex::Lock lock(mutex);
346 alive_flag=warm_target->add_frame(&surface);
347 if (!single_threaded()) cond_frame_queue_empty.signal();
352 /* === G L O B A L S ======================================================= */
354 /* === P R O C E D U R E S ================================================= */
356 /* === M E T H O D S ======================================================= */
358 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
365 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
367 etl::handle<AsyncTarget_Tile> wrap_target(
368 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
371 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
375 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
377 etl::handle<AsyncTarget_Scanline> wrap_target(
378 new AsyncTarget_Scanline(
379 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
383 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
389 AsyncRenderer::~AsyncRenderer()
395 AsyncRenderer::stop()
399 Glib::Mutex::Lock lock(mutex);
400 done_connection.disconnect();
407 if (!single_threaded()) render_thread->join();
410 // Make sure all the dispatch crap is cleared out
411 //Glib::MainContext::get_default()->iteration(false);
425 AsyncRenderer::pause()
430 AsyncRenderer::resume()
435 AsyncRenderer::start()
437 done_connection=Glib::signal_timeout().connect(
439 mem_fun(*this,&AsyncRenderer::start_),
447 AsyncRenderer::rendering_progress()
450 while(studio::App::events_pending()) studio::App::iteration(false);
455 AsyncRenderer::start_()
457 error=false;success=false;
460 #ifndef GLIB_DISPATCHER_BROKEN
461 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
464 if (single_threaded())
466 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
467 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
468 render_thread = (Glib::Thread*)1;
473 render_thread=Glib::Thread::create(
474 sigc::mem_fun(*this,&AsyncRenderer::render_target),
481 assert(render_thread);
491 AsyncRenderer::render_target()
493 etl::handle<Target> target(AsyncRenderer::target);
495 if(target && target->render())
502 #ifndef REJOIN_ON_STOP
509 #ifdef GLIB_DISPATCHER_BROKEN
510 done_connection=Glib::signal_timeout().connect(
512 mem_fun(*this,&AsyncRenderer::stop),