Get rid of all references to the bootstrap script since it was removed
[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 #include "general.h"
57
58 #endif
59
60 /* === U S I N G =========================================================== */
61
62 using namespace std;
63 using namespace etl;
64 using namespace synfig;
65 using namespace studio;
66
67 #define BOREDOM_TIMEOUT         50
68
69 #define REJOIN_ON_STOP  1
70
71 // The Glib::Dispatcher class is broken as of Glibmm 2.4.5.
72 // Defining this macro enables the workaround.
73 #define GLIB_DISPATCHER_BROKEN 1
74
75 /* === C L A S S E S ======================================================= */
76
77 class AsyncTarget_Tile : public synfig::Target_Tile
78 {
79 public:
80         etl::handle<synfig::Target_Tile> warm_target;
81
82         struct tile_t
83         {
84                 Surface surface;
85                 int x,y;
86                 tile_t(const Surface& surface,int x, int y):
87                         surface(surface),
88                         x(x),y(y)
89                 {
90                 }
91         };
92         std::list<tile_t> tile_queue;
93         Glib::Mutex mutex;
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                 Glib::Mutex::Lock lock(mutex);
129                 alive_flag=false;
130         }
131
132         virtual int total_tiles()const
133         {
134                 return warm_target->total_tiles();
135         }
136
137         virtual int next_tile(int& x, int& y)
138         {
139                 if(!alive_flag)
140                         return 0;
141
142                 return warm_target->next_tile(x,y);
143         }
144
145         virtual int next_frame(Time& time)
146         {
147                 if(!alive_flag)
148                         return 0;
149                 return warm_target->next_frame(time);
150         }
151
152         virtual bool start_frame(synfig::ProgressCallback *cb=0)
153         {
154                 if(!alive_flag)
155                         return false;
156                 return warm_target->start_frame(cb);
157         }
158
159         virtual bool add_tile(const synfig::Surface &surface, int gx, int gy)
160         {
161                 assert(surface);
162                 if(!alive_flag)
163                         return false;
164                 Glib::Mutex::Lock lock(mutex);
165                 tile_queue.push_back(tile_t(surface,gx,gy));
166                 if(tile_queue.size()==1)
167                 {
168 #ifdef GLIB_DISPATCHER_BROKEN
169                 ready_connection=Glib::signal_timeout().connect(
170                         sigc::bind_return(
171                                 sigc::mem_fun(*this,&AsyncTarget_Tile::tile_ready),
172                                 false
173                         )
174                         ,0
175                 );
176 #else
177                 tile_ready_signal();
178 #endif
179                 }
180
181                 return alive_flag;
182         }
183
184         void tile_ready()
185         {
186                 Glib::Mutex::Lock lock(mutex);
187                 if(!alive_flag)
188                 {
189                         tile_queue.clear();
190                         cond_tile_queue_empty.signal();
191                         return;
192                 }
193                 while(!tile_queue.empty() && alive_flag)
194                 {
195                         tile_t& tile(tile_queue.front());
196
197                         if (getenv("SYNFIG_SHOW_TILE_OUTLINES"))
198                         {
199                                 Color red(1,0,0);
200                                 tile.surface.fill(red, 0, 0, 1, tile.surface.get_h());
201                                 tile.surface.fill(red, 0, 0, tile.surface.get_w(), 1);
202                         }
203
204                         alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
205
206                         tile_queue.pop_front();
207                 }
208                 cond_tile_queue_empty.signal();
209         }
210
211         virtual void end_frame()
212         {
213                 if (!single_threaded())
214                 {
215                         while(alive_flag)
216                         {
217                                 Glib::Mutex::Lock lock(mutex);
218                                 if(!tile_queue.empty() && alive_flag)
219                                 {
220                                         if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
221                                                 break;
222                                 }
223                                 else
224                                         break;
225                         }
226                 }
227                 Glib::Mutex::Lock lock(mutex);
228                 if(!alive_flag)
229                         return;
230                 return warm_target->end_frame();
231         }
232 };
233
234
235
236 class AsyncTarget_Scanline : public synfig::Target_Scanline
237 {
238 public:
239         etl::handle<synfig::Target_Scanline> warm_target;
240
241         int scanline_;
242         Surface surface;
243
244         Glib::Mutex mutex;
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                 Glib::Mutex::Lock lock(mutex);
288                 alive_flag=false;
289         }
290
291         virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
292         {
293                 return alive_flag;
294         }
295
296         virtual void end_frame()
297         {
298                 {
299                         Glib::Mutex::Lock lock(mutex);
300
301                         if(!alive_flag)
302                                 return;
303                         ready_next=false;
304
305 #ifdef GLIB_DISPATCHER_BROKEN
306                 ready_connection=Glib::signal_timeout().connect(
307                         sigc::bind_return(
308                                 sigc::mem_fun(*this,&AsyncTarget_Scanline::frame_ready),
309                                 false
310                         )
311                         ,0
312                 );
313 #else
314                         frame_ready_signal();
315 #endif
316                 }
317
318                 if (single_threaded())
319                         signal_progress()();
320                 else
321                         while(alive_flag && !ready_next)
322                         {
323                                 Glib::Mutex::Lock lock(mutex);
324                                 if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
325                                         break;
326                         }
327         }
328
329
330         virtual Color * start_scanline(int scanline)
331         {
332                 Glib::Mutex::Lock lock(mutex);
333
334                 return surface[scanline];
335         }
336
337         virtual bool end_scanline()
338         {
339                 return alive_flag;
340         }
341
342         void frame_ready()
343         {
344                 Glib::Mutex::Lock lock(mutex);
345                 if(alive_flag)
346                         alive_flag=warm_target->add_frame(&surface);
347                 if (!single_threaded()) cond_frame_queue_empty.signal();
348                 ready_next=true;
349         }
350 };
351
352 /* === G L O B A L S ======================================================= */
353
354 /* === P R O C E D U R E S ================================================= */
355
356 /* === M E T H O D S ======================================================= */
357
358 AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::ProgressCallback *cb):
359         error(false),
360         success(false),
361         cb(cb),
362         updating(false)
363 {
364         render_thread=0;
365         if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
366         {
367                 etl::handle<AsyncTarget_Tile> wrap_target(
368                         new AsyncTarget_Tile(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
369                 );
370
371                 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Tile::set_dead));
372
373                 target=wrap_target;
374         }
375         else if(etl::handle<synfig::Target_Scanline>::cast_dynamic(target_))
376         {
377                 etl::handle<AsyncTarget_Scanline> wrap_target(
378                         new AsyncTarget_Scanline(
379                                 etl::handle<synfig::Target_Scanline>::cast_dynamic(target_)
380                         )
381                 );
382
383                 signal_stop_.connect(sigc::mem_fun(*wrap_target,&AsyncTarget_Scanline::set_dead));
384
385                 target=wrap_target;
386         }
387 }
388
389 AsyncRenderer::~AsyncRenderer()
390 {
391         stop();
392 }
393
394 void
395 AsyncRenderer::stop()
396 {
397         if(target)
398         {
399                 Glib::Mutex::Lock lock(mutex);
400                 done_connection.disconnect();
401
402                 if(render_thread)
403                 {
404                         signal_stop_();
405
406 #if REJOIN_ON_STOP
407                         if (!single_threaded()) render_thread->join();
408 #endif
409
410                         // Make sure all the dispatch crap is cleared out
411                         //Glib::MainContext::get_default()->iteration(false);
412
413                         if(success)
414                                 signal_success_();
415
416                         signal_finished_();
417
418                         target=0;
419                         render_thread=0;
420                 }
421         }
422 }
423
424 void
425 AsyncRenderer::pause()
426 {
427 }
428
429 void
430 AsyncRenderer::resume()
431 {
432 }
433
434 void
435 AsyncRenderer::start()
436 {
437         done_connection=Glib::signal_timeout().connect(
438                 sigc::bind_return(
439                         mem_fun(*this,&AsyncRenderer::start_),
440                         false
441                 )
442                 ,50
443         );
444 }
445
446 void
447 AsyncRenderer::rendering_progress()
448 {
449         updating = true;
450         while(studio::App::events_pending()) studio::App::iteration(false);
451         updating = false;
452 }
453
454 void
455 AsyncRenderer::start_()
456 {
457         error=false;success=false;
458         if(target)
459         {
460 #ifndef GLIB_DISPATCHER_BROKEN
461                 done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
462 #endif
463
464                 if (single_threaded())
465                 {
466                         synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
467                         target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
468                         render_thread = (Glib::Thread*)1;
469                         render_target();
470                 }
471                 else
472                 {
473                         render_thread=Glib::Thread::create(
474                                 sigc::mem_fun(*this,&AsyncRenderer::render_target),
475 #if REJOIN_ON_STOP
476                                 true
477 #else
478                                 false
479 #endif
480                                 );
481                         assert(render_thread);
482                 }
483         }
484         else
485         {
486                 stop();
487         }
488 }
489
490 void
491 AsyncRenderer::render_target()
492 {
493         etl::handle<Target> target(AsyncRenderer::target);
494
495         if(target && target->render())
496         {
497                 success=true;
498         }
499         else
500         {
501                 error=true;
502 #ifndef REJOIN_ON_STOP
503                 return;
504 #endif
505         }
506
507         if(mutex.trylock())
508         {
509 #ifdef GLIB_DISPATCHER_BROKEN
510                 done_connection=Glib::signal_timeout().connect(
511                         sigc::bind_return(
512                                 mem_fun(*this,&AsyncRenderer::stop),
513                                 false
514                         )
515                         ,0
516                 );
517 #else
518                 signal_done_.emit();
519 #endif
520                 mutex.unlock();
521         }
522 }