Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-studio / tags / stable / src / gtkmm / asyncrenderer.cpp
index 745409c..72a286c 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -30,6 +31,7 @@
 #endif
 
 #include "asyncrenderer.h"
+#include "app.h"
 #include <glibmm/thread.h>
 #include <glibmm/dispatcher.h>
 
@@ -52,6 +54,8 @@
 #include <synfig/general.h>
 #include <ETL/clock>
 
+#include "general.h"
+
 #endif
 
 /* === U S I N G =========================================================== */
@@ -191,6 +195,13 @@ public:
                {
                        tile_t& tile(tile_queue.front());
 
+                       if (getenv("SYNFIG_SHOW_TILE_OUTLINES"))
+                       {
+                               Color red(1,0,0);
+                               tile.surface.fill(red, 0, 0, 1, tile.surface.get_h());
+                               tile.surface.fill(red, 0, 0, tile.surface.get_w(), 1);
+                       }
+
                        alive_flag=warm_target->add_tile(tile.surface,tile.x,tile.y);
 
                        tile_queue.pop_front();
@@ -200,17 +211,24 @@ public:
 
        virtual void end_frame()
        {
-               while(alive_flag)
+#ifdef SINGLE_THREADED
+               if (!single_threaded())
                {
-                       Glib::Mutex::Lock lock(mutex);
-                       if(!tile_queue.empty() && alive_flag)
+#endif
+                       while(alive_flag)
                        {
-                               if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
+                               Glib::Mutex::Lock lock(mutex);
+                               if(!tile_queue.empty() && alive_flag)
+                               {
+                                       if(cond_tile_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
+                                               break;
+                               }
+                               else
                                        break;
                        }
-                       else
-                               break;
+#ifdef SINGLE_THREADED
                }
+#endif
                Glib::Mutex::Lock lock(mutex);
                if(!alive_flag)
                        return;
@@ -275,7 +293,7 @@ public:
                alive_flag=false;
        }
 
-       virtual bool start_frame(synfig::ProgressCallback *cb=0)
+       virtual bool start_frame(synfig::ProgressCallback */*cb*/=0)
        {
                return alive_flag;
        }
@@ -300,14 +318,19 @@ public:
 #else
                        frame_ready_signal();
 #endif
-                       }
-
-               while(alive_flag && !ready_next)
-               {
-                       Glib::Mutex::Lock lock(mutex);
-                       if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
-                               break;
                }
+
+#ifdef SINGLE_THREADED
+               if (single_threaded())
+                       signal_progress()();
+               else
+#endif
+                       while(alive_flag && !ready_next)
+                       {
+                               Glib::Mutex::Lock lock(mutex);
+                               if(cond_frame_queue_empty.timed_wait(mutex,Glib::TimeVal(0,BOREDOM_TIMEOUT)))
+                                       break;
+                       }
        }
 
 
@@ -328,7 +351,10 @@ public:
                Glib::Mutex::Lock lock(mutex);
                if(alive_flag)
                        alive_flag=warm_target->add_frame(&surface);
-               cond_frame_queue_empty.signal();
+#ifdef SINGLE_THREADED
+               if (!single_threaded())
+#endif
+                       cond_frame_queue_empty.signal();
                ready_next=true;
        }
 };
@@ -343,6 +369,9 @@ AsyncRenderer::AsyncRenderer(etl::handle<synfig::Target> target_,synfig::Progres
        error(false),
        success(false),
        cb(cb)
+#ifdef SINGLE_THREADED
+       , updating(false)
+#endif
 {
        render_thread=0;
        if(etl::handle<synfig::Target_Tile>::cast_dynamic(target_))
@@ -387,7 +416,10 @@ AsyncRenderer::stop()
                        signal_stop_();
 
 #if REJOIN_ON_STOP
-                       render_thread->join();
+#ifdef SINGLE_THREADED
+                       if (!single_threaded())
+#endif
+                               render_thread->join();
 #endif
 
                        // Make sure all the dispatch crap is cleared out
@@ -426,6 +458,16 @@ AsyncRenderer::start()
        );
 }
 
+#ifdef SINGLE_THREADED
+void
+AsyncRenderer::rendering_progress()
+{
+       updating = true;
+       while(studio::App::events_pending()) studio::App::iteration(false);
+       updating = false;
+}
+#endif
+
 void
 AsyncRenderer::start_()
 {
@@ -436,15 +478,27 @@ AsyncRenderer::start_()
                done_connection=signal_done_.connect(mem_fun(*this,&AsyncRenderer::stop));
 #endif
 
-               render_thread=Glib::Thread::create(
-                       sigc::mem_fun(*this,&AsyncRenderer::render_target),
+#ifdef SINGLE_THREADED
+               if (single_threaded())
+               {
+                       synfig::info("%s:%d rendering in the same thread", __FILE__, __LINE__);
+                       target->signal_progress().connect(sigc::mem_fun(this,&AsyncRenderer::rendering_progress));
+                       render_thread = (Glib::Thread*)1;
+                       render_target();
+               }
+               else
+#endif
+               {
+                       render_thread=Glib::Thread::create(
+                               sigc::mem_fun(*this,&AsyncRenderer::render_target),
 #if REJOIN_ON_STOP
-                       true
+                               true
 #else
-                       false
+                               false
 #endif
-               );
-               assert(render_thread);
+                               );
+                       assert(render_thread);
+               }
        }
        else
        {