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 if (!single_threaded())
218 Glib::Mutex::Lock lock(mutex);
219 if(!tile_queue.empty() && alive_flag)
221 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
228 Glib::Mutex::Lock lock(mutex);
231 return warm_target->end_frame();
237 class AsyncTarget_Scanline : public synfig::Target_Scanline
240 etl::handle<synfig::Target_Scanline> warm_target;
247 #ifndef GLIB_DISPATCHER_BROKEN
248 Glib::Dispatcher frame_ready_signal;
250 Glib::Cond cond_frame_queue_empty;
253 sigc::connection ready_connection;
257 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
258 warm_target(warm_target)
260 set_avoid_time_sync(warm_target->get_avoid_time_sync());
261 set_canvas(warm_target->get_canvas());
262 set_quality(warm_target->get_quality());
263 set_remove_alpha(warm_target->get_remove_alpha());
264 set_threads(warm_target->get_threads());
265 set_rend_desc(&warm_target->rend_desc());
267 #ifndef GLIB_DISPATCHER_BROKEN
268 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
270 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
273 ~AsyncTarget_Scanline()
275 ready_connection.disconnect();
278 virtual int next_frame(Time& time)
282 return warm_target->next_frame(time);
288 Glib::Mutex::Lock lock(mutex);
292 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
297 virtual void end_frame()
300 Glib::Mutex::Lock lock(mutex);
306 #ifdef GLIB_DISPATCHER_BROKEN
307 ready_connection=Glib::signal_timeout().connect(
309 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
315 frame_ready_signal();
319 if (single_threaded())
322 while(alive_flag && !ready_next)
324 Glib::Mutex::Lock lock(mutex);
325 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
331 virtual Color * start_scanline(int scanline)
333 Glib::Mutex::Lock lock(mutex);
335 return surface[scanline];
338 virtual bool end_scanline()
345 Glib::Mutex::Lock lock(mutex);
347 alive_flag=warm_target->add_frame(&surface);
348 if (!single_threaded()) cond_frame_queue_empty.signal();
353 /* === G L O B A L S ======================================================= */
355 /* === P R O C E D U R E S ================================================= */
357 /* === M E T H O D S ======================================================= */
359 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
366 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
368 etl::handle<AsyncTarget_Tile> wrap_target(
369 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
372 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
376 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
378 etl::handle<AsyncTarget_Scanline> wrap_target(
379 new AsyncTarget_Scanline(
380 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
384 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
390 AsyncRenderer::~AsyncRenderer()
396 AsyncRenderer::stop()
400 Glib::Mutex::Lock lock(mutex);
401 done_connection.disconnect();
408 if (!single_threaded()) render_thread->join();
411 // Make sure all the dispatch crap is cleared out
412 //Glib::MainContext::get_default()->iteration(false);
426 AsyncRenderer::pause()
431 AsyncRenderer::resume()
436 AsyncRenderer::start()
438 done_connection=Glib::signal_timeout().connect(
440 mem_fun(*this,&AsyncRenderer::start_),
448 AsyncRenderer::rendering_progress()
451 while(studio::App::events_pending()) studio::App::iteration(false);
456 AsyncRenderer::start_()
458 error=false;success=false;
461 #ifndef GLIB_DISPATCHER_BROKEN
462 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
465 if (single_threaded())
467 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
468 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
469 render_thread = (Glib::Thread*)1;
474 render_thread=Glib::Thread::create(
475 sigc::mem_fun(*this,&AsyncRenderer::render_target),
482 assert(render_thread);
492 AsyncRenderer::render_target()
494 etl::handle<Target> target(AsyncRenderer::target);
496 if(target && target->render())
503 #ifndef REJOIN_ON_STOP
510 #ifdef GLIB_DISPATCHER_BROKEN
511 done_connection=Glib::signal_timeout().connect(
513 mem_fun(*this,&AsyncRenderer::stop),