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);
130 virtual int total_tiles()const
132 return warm_target->total_tiles();
135 virtual int next_tile(int& x, int& y)
140 return warm_target->next_tile(x,y);
143 virtual int next_frame(Time& time)
147 return warm_target->next_frame(time);
150 virtual bool start_frame(synfig::ProgressCallback *cb=0)
154 return warm_target->start_frame(cb);
157 virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
162 Glib::Mutex::Lock lock(mutex);
163 tile_queue.push_back(tile_t(surface,gx,gy));
164 if(tile_queue.size()==1)
166 #ifdef GLIB_DISPATCHER_BROKEN
167 ready_connection=Glib::signal_timeout().connect(
169 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
184 Glib::Mutex::Lock lock(mutex);
188 cond_tile_queue_empty.signal();
191 while(!tile_queue.empty() && alive_flag)
193 tile_t& tile(tile_queue.front());
195 alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
197 tile_queue.pop_front();
199 cond_tile_queue_empty.signal();
202 virtual void end_frame()
204 if (!single_threaded())
208 Glib::Mutex::Lock lock(mutex);
209 if(!tile_queue.empty() && alive_flag)
211 if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
218 Glib::Mutex::Lock lock(mutex);
221 return warm_target->end_frame();
227 class AsyncTarget_Scanline : public synfig::Target_Scanline
230 etl::handle<synfig::Target_Scanline> warm_target;
237 #ifndef GLIB_DISPATCHER_BROKEN
238 Glib::Dispatcher frame_ready_signal;
240 Glib::Cond cond_frame_queue_empty;
243 sigc::connection ready_connection;
247 AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
248 warm_target(warm_target)
250 set_avoid_time_sync(warm_target->get_avoid_time_sync());
251 set_canvas(warm_target->get_canvas());
252 set_quality(warm_target->get_quality());
253 set_remove_alpha(warm_target->get_remove_alpha());
254 set_threads(warm_target->get_threads());
255 set_rend_desc(&warm_target->rend_desc());
257 #ifndef GLIB_DISPATCHER_BROKEN
258 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
260 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
263 ~AsyncTarget_Scanline()
265 ready_connection.disconnect();
268 virtual int next_frame(Time& time)
272 return warm_target->next_frame(time);
278 Glib::Mutex::Lock lock(mutex);
282 virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
287 virtual void end_frame()
290 Glib::Mutex::Lock lock(mutex);
296 #ifdef GLIB_DISPATCHER_BROKEN
297 ready_connection=Glib::signal_timeout().connect(
299 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
305 frame_ready_signal();
309 if (single_threaded())
312 while(alive_flag && !ready_next)
314 Glib::Mutex::Lock lock(mutex);
315 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
321 virtual Color * start_scanline(int scanline)
323 Glib::Mutex::Lock lock(mutex);
325 return surface[scanline];
328 virtual bool end_scanline()
335 Glib::Mutex::Lock lock(mutex);
337 alive_flag=warm_target->add_frame(&surface);
338 if (!single_threaded()) cond_frame_queue_empty.signal();
343 /* === G L O B A L S ======================================================= */
345 /* === P R O C E D U R E S ================================================= */
347 /* === M E T H O D S ======================================================= */
349 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
356 if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
358 etl::handle<AsyncTarget_Tile> wrap_target(
359 new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
362 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
366 else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
368 etl::handle<AsyncTarget_Scanline> wrap_target(
369 new AsyncTarget_Scanline(
370 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
374 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
380 AsyncRenderer::~AsyncRenderer()
386 AsyncRenderer::stop()
390 Glib::Mutex::Lock lock(mutex);
391 done_connection.disconnect();
398 if (!single_threaded()) render_thread->join();
401 // Make sure all the dispatch crap is cleared out
402 //Glib::MainContext::get_default()->iteration(false);
416 AsyncRenderer::pause()
421 AsyncRenderer::resume()
426 AsyncRenderer::start()
428 done_connection=Glib::signal_timeout().connect(
430 mem_fun(*this,&AsyncRenderer::start_),
438 AsyncRenderer::rendering_progress()
441 while(studio::App::events_pending()) studio::App::iteration(false);
446 AsyncRenderer::start_()
448 error=false;success=false;
451 #ifndef GLIB_DISPATCHER_BROKEN
452 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
455 if (single_threaded())
457 synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
458 target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
459 render_thread = (Glib::Thread*)1;
464 render_thread=Glib::Thread::create(
465 sigc::mem_fun(*this,&AsyncRenderer::render_target),
472 assert(render_thread);
482 AsyncRenderer::render_target()
484 etl::handle<Target> target(AsyncRenderer::target);
486 if(target && target->render())
493 #ifndef REJOIN_ON_STOP
500 #ifdef GLIB_DISPATCHER_BROKEN
501 done_connection=Glib::signal_timeout().connect(
503 mem_fun(*this,&AsyncRenderer::stop),