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