00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "ScribbleSaveThread.h"
00038 #include "ScribbleLog.h"
00039 #include "ScribbleUtils.h"
00040
00041
00042 GMutex* imgdata_mutex = NULL;
00043
00044 GCond* data_cond = NULL;
00045
00046 static GThread* s_threadSave=NULL;
00047
00048 static gboolean s_bQuit=FALSE;
00049
00050 static gboolean s_bNeedSaveResultPage=FALSE;
00051
00052
00053
00054
00055 GdkPixbuf *s_pixbuf=NULL;
00056
00057 void saveThreadMain(gpointer data)
00058 {
00059 while( !s_bQuit )
00060 {
00061 g_mutex_lock(imgdata_mutex);
00062 while(!s_bNeedSaveResultPage && !s_bQuit)
00063 g_cond_wait (data_cond, imgdata_mutex);
00064
00065 if(s_bQuit)
00066 {
00067 g_mutex_unlock(imgdata_mutex);
00068 break;
00069 }
00070 SB_TIMEDISPLAY("get save request\n");
00071 char sResultImage[MAX_FILENAME];
00072 PScribbleUICtx pScribbleUICtx=(PScribbleUICtx)data;
00073 PFileManager pFM=&pScribbleUICtx->fileManager;
00074 get_curr_result_filename(sResultImage,MAX_FILENAME,pFM);
00075
00076 s_bNeedSaveResultPage=FALSE;
00077 SB_TIMEDISPLAY("start save\n");
00078
00079 util_savePixbuf(s_pixbuf,sResultImage,TYPE_PNG);
00080
00081 g_object_unref (s_pixbuf);
00082 g_mutex_unlock(imgdata_mutex);
00083 SB_TIMEDISPLAY("end save\n");
00084 }
00085 }
00086
00087
00088 int initSaveThread(ScribbleUICtx *pScribbleUICtx)
00089 {
00090
00091 imgdata_mutex=g_mutex_new();
00092 if(NULL==imgdata_mutex)
00093 {
00094 SB_ERRORPRINTF("imgdata_mutex create error\n");
00095 return -1;
00096 }
00097 data_cond=g_cond_new();
00098 if(NULL==data_cond)
00099 {
00100 SB_ERRORPRINTF("data_cond create error\n");
00101 return -1;
00102 }
00103
00104 GError *error=NULL;
00105 s_threadSave=g_thread_create((GThreadFunc)saveThreadMain,
00106 pScribbleUICtx,TRUE,&error);
00107 if(NULL==s_threadSave)
00108 {
00109 SB_ERRORPRINTF("thread create error\n");
00110 return -1;
00111 }
00112 return 0;
00113 }
00114
00115
00116 void notifySave(PScribbleUICtx pScribbleUICtx)
00117 {
00118 SB_TIMEDISPLAY("start\n");
00119
00120 g_mutex_lock(imgdata_mutex);
00121 SB_TIMEDISPLAY("fetch pixbuf\n");
00122 s_pixbuf = gdk_pixbuf_get_from_drawable(NULL,
00123 (GdkDrawable *)pScribbleUICtx->pixmap,
00124 NULL,0,0,0,0,-1,-1);
00125 SB_TIMEDISPLAY("end fetch\n");
00126 s_bNeedSaveResultPage=TRUE;
00127 g_cond_signal(data_cond);
00128 g_mutex_unlock(imgdata_mutex);
00129 SB_TIMEDISPLAY("end\n");
00130 }
00131
00132
00133 void exitSaveThread()
00134 {
00135 SB_TIMEDISPLAY("save-thread ready to exit\n");
00136 g_mutex_lock(imgdata_mutex);
00137 s_bQuit=TRUE;
00138 g_cond_signal(data_cond);
00139 g_mutex_unlock(imgdata_mutex);
00140
00141 g_thread_join(s_threadSave);
00142 SB_TIMEDISPLAY("save-thread exit\n");
00143
00144
00145
00146 }
00147