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, NotLock);
127 if (!single_threaded()) lock.acquire();
131 virtual int total_tiles()const
133 return warm_target->total_tiles();
136 virtual int next_tile(int& x, int& y)
141 return warm_target->next_tile(x,y);
144 virtual int next_frame(Time& time)
148 return warm_target->next_frame(time);
151 virtual bool start_frame(synfig::ProgressCallback *cb=0)
155 return warm_target->start_frame(cb);
158 virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
163 Glib::Mutex::Lock lock(mutex, NotLock);
164 if (!single_threaded()) lock.acquire();
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, NotLock);
187 if (!single_threaded()) lock.acquire();
191 cond_tile_queue_empty.signal();
194 while(!tile_queue.empty() && alive_flag)
196 tile_t& tile(tile_queue.front());
198 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
200 tile_queue.pop_front();
202 cond_tile_queue_empty.signal();
205 virtual void end_frame()
207 if (!single_threaded())
211 Glib::Mutex::Lock lock(mutex);
212 if(!tile_queue.empty() && alive_flag)
214 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
221 Glib::Mutex::Lock lock(mutex, NotLock);
222 if (!single_threaded()) lock.acquire();
225 return warm_target->end_frame();
231 class AsyncTarget_Scanline : public synfig::Target_Scanline
234 etl::handle<synfig::Target_Scanline> warm_target;
241 #ifndef GLIB_DISPATCHER_BROKEN
242 Glib::Dispatcher frame_ready_signal;
244 Glib::Cond cond_frame_queue_empty;
247 sigc::connection ready_connection;
251 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
252 warm_target(warm_target)
254 set_avoid_time_sync(warm_target->get_avoid_time_sync());
255 set_canvas(warm_target->get_canvas());
256 set_quality(warm_target->get_quality());
257 set_remove_alpha(warm_target->get_remove_alpha());
258 set_threads(warm_target->get_threads());
259 set_rend_desc(&warm_target->rend_desc());
261 #ifndef GLIB_DISPATCHER_BROKEN
262 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
264 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
267 ~AsyncTarget_Scanline()
269 ready_connection.disconnect();
272 virtual int next_frame(Time& time)
276 return warm_target->next_frame(time);
282 Glib::Mutex::Lock lock(mutex, NotLock);
283 if (!single_threaded()) lock.acquire();
287 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
292 virtual void end_frame()
295 Glib::Mutex::Lock lock(mutex, NotLock);
296 if (!single_threaded()) lock.acquire();
302 #ifdef GLIB_DISPATCHER_BROKEN
303 ready_connection=Glib::signal_timeout().connect(
305 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
311 frame_ready_signal();
315 if (!single_threaded())
316 while(alive_flag && !ready_next)
318 Glib::Mutex::Lock lock(mutex);
319 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
325 virtual Color * start_scanline(int scanline)
327 Glib::Mutex::Lock lock(mutex, NotLock);
328 if (!single_threaded()) lock.acquire();
330 return surface[scanline];
333 virtual bool end_scanline()
340 Glib::Mutex::Lock lock(mutex, NotLock);
341 if (!single_threaded()) lock.acquire();
343 alive_flag=warm_target->add_frame(&surface);
344 if (!single_threaded()) cond_frame_queue_empty.signal();
349 /* === G L O B A L S ======================================================= */
351 /* === P R O C E D U R E S ================================================= */
353 /* === M E T H O D S ======================================================= */
355 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
362 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
364 etl::handle<AsyncTarget_Tile> wrap_target(
365 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
368 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
372 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
374 etl::handle<AsyncTarget_Scanline> wrap_target(
375 new AsyncTarget_Scanline(
376 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
380 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
386 AsyncRenderer::~AsyncRenderer()
392 AsyncRenderer::stop()
396 Glib::Mutex::Lock lock(mutex, NotLock);
397 if (!single_threaded()) lock.acquire();
398 done_connection.disconnect();
405 if (!single_threaded()) render_thread->join();
408 // Make sure all the dispatch crap is cleared out
409 //Glib::MainContext::get_default()->iteration(false);
423 AsyncRenderer::pause()
428 AsyncRenderer::resume()
433 AsyncRenderer::start()
435 done_connection=Glib::signal_timeout().connect(
437 mem_fun(*this,&AsyncRenderer::start_),
445 AsyncRenderer::rendering_progress()
448 while(studio::App::events_pending()) studio::App::iteration(false);
453 AsyncRenderer::start_()
455 error=false;success=false;
458 #ifndef GLIB_DISPATCHER_BROKEN
459 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
462 if (single_threaded())
464 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
465 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
466 render_thread = (Glib::Thread*)1;
471 render_thread=Glib::Thread::create(
472 sigc::mem_fun(*this,&AsyncRenderer::render_target),
479 assert(render_thread);
489 AsyncRenderer::render_target()
491 etl::handle<Target> target(AsyncRenderer::target);
493 if(target && target->render())
500 #ifndef REJOIN_ON_STOP
505 if (single_threaded() || mutex.trylock())
507 #ifdef GLIB_DISPATCHER_BROKEN
508 done_connection=Glib::signal_timeout().connect(
510 mem_fun(*this,&AsyncRenderer::stop),
518 if (!single_threaded()) mutex.unlock();