72a286c92a56c1dfb5d7009284288a61d6de07d9
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file asyncrenderer.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
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.
15 **
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.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "asyncrenderer.h"
34 #include "app.h"
35 #include <glibmm/thread.h>
36 #include <glibmm/dispatcher.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
44 #endif
45
46 #ifdef HAVE_SYS_WAIT_H
47 #include <sys/wait.h>
48 #endif
49
50 #ifdef HAVE_SIGNAL_H
51 #include <signal.h>
52 #endif
53
54 #include <synfig/general.h>
55 #include <ETL/clock>
56
57 #include "general.h"
58
59 #endif
60
61 /* === U S I N G =========================================================== */
62
63 using namespace std;
64 using namespace etl;
65 using namespace synfig;
66 using namespace studio;
67
68 #define BOREDOM_TIMEOUT         50
69
70 #define REJOIN_ON_STOP  1
71
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
75
76 /* === C L A S S E S ======================================================= */
77
78 class AsyncTarget_Tile : public synfig::Target_Tile
79 {
80 public:
81         etl::handle<synfig::Target_Tile> warm_target;
82
83         struct tile_t
84         {
85                 Surface surface;
86                 int x,y;
87                 tile_t(const Surface& surface,int x, int y):
88                         surface(surface),
89                         x(x),y(y)
90                 {
91                 }
92         };
93         std::list<tile_t> tile_queue;
94         Glib::Mutex mutex;
95
96 #ifndef GLIB_DISPATCHER_BROKEN
97         Glib::Dispatcher tile_ready_signal;
98 #endif
99         Glib::Cond cond_tile_queue_empty;
100         bool alive_flag;
101
102         sigc::connection ready_connection;
103
104 public:
105         AsyncTarget_Tile(etl::handle<synfig::Target_Tile> warm_target):
106                 warm_target(warm_target)
107         {
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());
117                 alive_flag=true;
118 #ifndef GLIB_DISPATCHER_BROKEN
119                 ready_connection=tile_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready));
120 #endif
121         }
122
123         ~AsyncTarget_Tile()
124         {
125                 ready_connection.disconnect();
126         }
127         void set_dead()
128         {
129                 Glib::Mutex::Lock lock(mutex);
130                 alive_flag=false;
131         }
132
133         virtual int total_tiles()const
134         {
135                 return warm_target->total_tiles();
136         }
137
138         virtual int next_tile(int& x, int& y)
139         {
140                 if(!alive_flag)
141                         return 0;
142
143                 return warm_target->next_tile(x,y);
144         }
145
146         virtual int next_frame(Time& time)
147         {
148                 if(!alive_flag)
149                         return 0;
150                 return warm_target->next_frame(time);
151         }
152
153         virtual bool start_frame(synfig::ProgressCallback *cb=0)
154         {
155                 if(!alive_flag)
156                         return false;
157                 return warm_target->start_frame(cb);
158         }
159
160         virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
161         {
162                 assert(surface);
163                 if(!alive_flag)
164                         return false;
165                 Glib::Mutex::Lock lock(mutex);
166                 tile_queue.push_back(tile_t(surface,gx,gy));
167                 if(tile_queue.size()==1)
168                 {
169 #ifdef GLIB_DISPATCHER_BROKEN
170                 ready_connection=Glib::signal_timeout().connect(
171                         sigc::bind_return(
172                                 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
173                                 false
174                         )
175                         ,0
176                 );
177 #else
178                 tile_ready_signal();
179 #endif
180                 }
181
182                 return alive_flag;
183         }
184
185         void tile_ready()
186         {
187                 Glib::Mutex::Lock lock(mutex);
188                 if(!alive_flag)
189                 {
190                         tile_queue.clear();
191                         cond_tile_queue_empty.signal();
192                         return;
193                 }
194                 while(!tile_queue.empty() && alive_flag)
195                 {
196                         tile_t& tile(tile_queue.front());
197
198                         if (getenv("SYNFIG_SHOW_TILE_OUTLINES"))
199                         {
200                                 Color red(1,0,0);
201                                 tile.surface.fill(red, 0, 0, 1, tile.surface.get_h());
202                                 tile.surface.fill(red, 0, 0, tile.surface.get_w(), 1);
203                         }
204
205                         alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
206
207                         tile_queue.pop_front();
208                 }
209                 cond_tile_queue_empty.signal();
210         }
211
212         virtual void end_frame()
213         {
214 #ifdef SINGLE_THREADED
215                 if (!single_threaded())
216                 {
217 #endif
218                         while(alive_flag)
219                         {
220                                 Glib::Mutex::Lock lock(mutex);
221                                 if(!tile_queue.empty() && alive_flag)
222                                 {
223                                         if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
224                                                 break;
225                                 }
226                                 else
227                                         break;
228                         }
229 #ifdef SINGLE_THREADED
230                 }
231 #endif
232                 Glib::Mutex::Lock lock(mutex);
233                 if(!alive_flag)
234                         return;
235                 return warm_target->end_frame();
236         }
237 };
238
239
240
241 class AsyncTarget_Scanline : public synfig::Target_Scanline
242 {
243 public:
244         etl::handle<synfig::Target_Scanline> warm_target;
245
246         int scanline_;
247         Surface surface;
248
249         Glib::Mutex mutex;
250
251 #ifndef GLIB_DISPATCHER_BROKEN
252         Glib::Dispatcher frame_ready_signal;
253 #endif
254         Glib::Cond cond_frame_queue_empty;
255         bool alive_flag;
256         bool ready_next;
257         sigc::connection ready_connection;
258
259
260 public:
261         AsyncTarget_Scanline(etl::handle<synfig::Target_Scanline> warm_target):
262                 warm_target(warm_target)
263         {
264                 set_avoid_time_sync(warm_target->get_avoid_time_sync());
265                 set_canvas(warm_target->get_canvas());
266                 set_quality(warm_target->get_quality());
267                 set_remove_alpha(warm_target->get_remove_alpha());
268                 set_threads(warm_target->get_threads());
269                 set_rend_desc(&warm_target->rend_desc());
270                 alive_flag=true;
271 #ifndef GLIB_DISPATCHER_BROKEN
272                 ready_connection=frame_ready_signal.connect(sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready));
273 #endif
274                 surface.set_wh(warm_target->rend_desc().get_w(),warm_target->rend_desc().get_h());
275         }
276
277         ~AsyncTarget_Scanline()
278         {
279                 ready_connection.disconnect();
280         }
281
282         virtual int next_frame(Time& time)
283         {
284                 if(!alive_flag)
285                         return 0;
286                 return warm_target->next_frame(time);
287
288         }
289
290         void set_dead()
291         {
292                 Glib::Mutex::Lock lock(mutex);
293                 alive_flag=false;
294         }
295
296         virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
297         {
298                 return alive_flag;
299         }
300
301         virtual void end_frame()
302         {
303                 {
304                         Glib::Mutex::Lock lock(mutex);
305
306                         if(!alive_flag)
307                                 return;
308                         ready_next=false;
309
310 #ifdef GLIB_DISPATCHER_BROKEN
311                 ready_connection=Glib::signal_timeout().connect(
312                         sigc::bind_return(
313                                 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
314                                 false
315                         )
316                         ,0
317                 );
318 #else
319                         frame_ready_signal();
320 #endif
321                 }
322
323 #ifdef SINGLE_THREADED
324                 if (single_threaded())
325                         signal_progress()();
326                 else
327 #endif
328                         while(alive_flag && !ready_next)
329                         {
330                                 Glib::Mutex::Lock lock(mutex);
331                                 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
332                                         break;
333                         }
334         }
335
336
337         virtual Color * start_scanline(int scanline)
338         {
339                 Glib::Mutex::Lock lock(mutex);
340
341                 return surface[scanline];
342         }
343
344         virtual bool end_scanline()
345         {
346                 return alive_flag;
347         }
348
349         void frame_ready()
350         {
351                 Glib::Mutex::Lock lock(mutex);
352                 if(alive_flag)
353                         alive_flag=warm_target->add_frame(&surface);
354 #ifdef SINGLE_THREADED
355                 if (!single_threaded())
356 #endif
357                         cond_frame_queue_empty.signal();
358                 ready_next=true;
359         }
360 };
361
362 /* === G L O B A L S ======================================================= */
363
364 /* === P R O C E D U R E S ================================================= */
365
366 /* === M E T H O D S ======================================================= */
367
368 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
369         error(false),
370         success(false),
371         cb(cb)
372 #ifdef SINGLE_THREADED
373         , updating(false)
374 #endif
375 {
376         render_thread=0;
377         if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
378         {
379                 etl::handle<AsyncTarget_Tile> wrap_target(
380                         new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
381                 );
382
383                 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
384
385                 target=wrap_target;
386         }
387         else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
388         {
389                 etl::handle<AsyncTarget_Scanline> wrap_target(
390                         new AsyncTarget_Scanline(
391                                 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
392                         )
393                 );
394
395                 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
396
397                 target=wrap_target;
398         }
399 }
400
401 AsyncRenderer::~AsyncRenderer()
402 {
403         stop();
404 }
405
406 void
407 AsyncRenderer::stop()
408 {
409         if(target)
410         {
411                 Glib::Mutex::Lock lock(mutex);
412                 done_connection.disconnect();
413
414                 if(render_thread)
415                 {
416                         signal_stop_();
417
418 #if REJOIN_ON_STOP
419 #ifdef SINGLE_THREADED
420                         if (!single_threaded())
421 #endif
422                                 render_thread->join();
423 #endif
424
425                         // Make sure all the dispatch crap is cleared out
426                         //Glib::MainContext::get_default()->iteration(false);
427
428                         if(success)
429                                 signal_success_();
430
431                         signal_finished_();
432
433                         target=0;
434                         render_thread=0;
435                 }
436         }
437 }
438
439 void
440 AsyncRenderer::pause()
441 {
442 }
443
444 void
445 AsyncRenderer::resume()
446 {
447 }
448
449 void
450 AsyncRenderer::start()
451 {
452         done_connection=Glib::signal_timeout().connect(
453                 sigc::bind_return(
454                         mem_fun(*this,&AsyncRenderer::start_),
455                         false
456                 )
457                 ,50
458         );
459 }
460
461 #ifdef SINGLE_THREADED
462 void
463 AsyncRenderer::rendering_progress()
464 {
465         updating = true;
466         while(studio::App::events_pending()) studio::App::iteration(false);
467         updating = false;
468 }
469 #endif
470
471 void
472 AsyncRenderer::start_()
473 {
474         error=false;success=false;
475         if(target)
476         {
477 #ifndef GLIB_DISPATCHER_BROKEN
478                 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
479 #endif
480
481 #ifdef SINGLE_THREADED
482                 if (single_threaded())
483                 {
484                         synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
485                         target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
486                         render_thread = (Glib::Thread*)1;
487                         render_target();
488                 }
489                 else
490 #endif
491                 {
492                         render_thread=Glib::Thread::create(
493                                 sigc::mem_fun(*this,&AsyncRenderer::render_target),
494 #if REJOIN_ON_STOP
495                                 true
496 #else
497                                 false
498 #endif
499                                 );
500                         assert(render_thread);
501                 }
502         }
503         else
504         {
505                 stop();
506         }
507 }
508
509 void
510 AsyncRenderer::render_target()
511 {
512         etl::handle<Target> target(AsyncRenderer::target);
513
514         if(target && target->render())
515         {
516                 success=true;
517         }
518         else
519         {
520                 error=true;
521 #ifndef REJOIN_ON_STOP
522                 return;
523 #endif
524         }
525
526         if(mutex.trylock())
527         {
528 #ifdef GLIB_DISPATCHER_BROKEN
529                 done_connection=Glib::signal_timeout().connect(
530                         sigc::bind_return(
531                                 mem_fun(*this,&AsyncRenderer::stop),
532                                 false
533                         )
534                         ,0
535                 );
536 #else
537                 signal_done_.emit();
538 #endif
539                 mutex.unlock();
540         }
541 }