CRenderThread Class Reference

#include <RenderThread.h>

Collaboration diagram for CRenderThread:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 CRenderThread (void)
 ~CRenderThread (void)
void start (ThreadParam *param)
void stop ()
void clearTodoList ()
void addTodoItem (TodoItem *item, GBool bHead=gTrue)
void signal ()
void renderPageNow (TodoItem *item)
GBool isInRendering (TodoItem *item)

Private Types

enum  threadState { Initialized = 0, Rendering = 1, RenderFinished = 2, ToAbort = 3 }

Private Member Functions

void initialize ()
void destroy ()
void adjustPageList (const int center)

Static Private Member Functions

static void * threadProc (void *)
static GBool abortCheckFunc (void *data)

Private Attributes

pthread_t thread
pthread_cond_t cond
pthread_mutex_t mutex
pthread_mutex_t abort
GBool bRunning
CPageInforenderingPage
ThreadParamparam

Static Private Attributes

static threadState abortState = Initialized


Detailed Description

Definition at line 40 of file RenderThread.h.


Member Enumeration Documentation

Enumerator:
Initialized 
Rendering 
RenderFinished 
ToAbort 

Definition at line 43 of file RenderThread.h.

00044     {
00045         Initialized     = 0, 
00046         Rendering       = 1, 
00047         RenderFinished  = 2,
00048         ToAbort         = 3,
00049     };


Constructor & Destructor Documentation

CRenderThread::CRenderThread ( void   ) 

Definition at line 28 of file RenderThread.cpp.

00029 : bRunning(gFalse)
00030 {
00031     initialize();
00032 }

Here is the call graph for this function:

CRenderThread::~CRenderThread ( void   ) 

Definition at line 34 of file RenderThread.cpp.

00035 {
00036     destroy();
00037 }

Here is the call graph for this function:


Member Function Documentation

void CRenderThread::initialize (  )  [private]

Definition at line 41 of file RenderThread.cpp.

00042 {
00043     pthread_mutex_init(&mutex, NULL);
00044     pthread_cond_init(&cond, NULL);
00045     pthread_mutex_init(&abort, NULL);
00046     renderingPage = NULL;
00047 }

void CRenderThread::destroy (  )  [private]

Definition at line 49 of file RenderThread.cpp.

00050 {
00051     pthread_mutex_destroy(&mutex);
00052     pthread_cond_destroy(&cond);
00053     pthread_mutex_destroy(&abort);
00054 }

void CRenderThread::adjustPageList ( const int  center  )  [private]

Definition at line 129 of file RenderThread.cpp.

00130 {
00131     // memory       allocate         pages
00132     // 50MB  ------- 20 MB   ------   35 
00133     // 30MB  ------- 30 MB   ------    8 ( 250%)
00134     static const int MEM = 1024 * 1024 * 15;    // 49.1 MB used from experiment, ~ 35 pages cached
00135 
00136     {
00137         char * p = (char *)malloc(MEM);
00138         if (p)
00139         {
00140             // PV_MEM("Memory is enough %d bytes!", MEM);
00141             free(p);
00142             return;
00143         }
00144         else 
00145         {
00146             // PV_MEM("Not enough memory, try to release some pages!");
00147             for(int i = 0; i < RemovePages; ++i)
00148             {
00149                 param->pageList->removeOldestOne(current);
00150             }                
00151         }
00152     }
00153 }

Here is the call graph for this function:

void * CRenderThread::threadProc ( void *  pvoid  )  [static, private]

Definition at line 225 of file RenderThread.cpp.

00226 {
00227     // install 
00228     installThreadTermHandler();
00229 
00230 
00231     // param
00232     ThreadParam * param = (ThreadParam *)pvoid;
00233 
00234     // loop
00235     for(;;)
00236     {
00237         if (param->bQuit)
00238         {
00239             PV_LOGPRINTF("Quit from thread!");
00240             return NULL;
00241         }
00242         
00243         // check todo list
00244         TodoItem *pItem = param->todoList->popItem();
00245         if (NULL == pItem) 
00246         {
00247             pthread_mutex_lock(&param->thread->mutex);
00248             PV_LOGPRINTF("Waiting...");
00249             pthread_cond_wait(&param->thread->cond, &param->thread->mutex); 
00250             pthread_mutex_unlock(&param->thread->mutex);
00251             continue;   // quit or do something 
00252         }
00253     
00254         // check page list 
00255         CPageInfo * page = param->pageList->checkPage(pItem);
00256         if (page) 
00257         {
00258             delete pItem;
00259             continue;
00260         }
00261         
00262         // memory control
00263         param->thread->adjustPageList(param->ctrl->settings.getCurrentPage());
00264                     
00265         // construct a new page item
00266         page = new CPageInfo;
00267         page->pageNumber = pItem->pageNumber;
00268         page->pageZoom   = pItem->zoom;
00269         page->rotate     = pItem->rotate;   // global settings
00270         page->timestamp  = pItem->timeStamp;
00271         delete pItem;
00272         
00273         // protect, renderingPage could be accessed by main thread and helper thread in same time
00274         pthread_mutex_lock(&param->thread->abort);
00275         param->thread->renderingPage = page;
00276         param->thread->abortState = Rendering;
00277         pthread_mutex_unlock(&param->thread->abort);
00278         
00279         // go
00280         RenderRet ret = param->ctrl->core->renderPage(*page, 
00281                 gFalse, gTrue, gTrue, abortCheckFunc, NULL);
00282     
00283         // reset                
00284         pthread_mutex_lock(&param->thread->abort);
00285         param->thread->renderingPage = NULL;
00286         param->thread->abortState = RenderFinished;
00287         pthread_mutex_unlock(&param->thread->abort);                                        
00288 
00289         // check result
00290         if (ret != Render_Done)
00291         {
00292             if (ret == Render_Abort)
00293             {
00294                 PV_LOGPRINTF("Page %d is aborted!", page->pageNumber);
00295             }
00296             delete page;
00297             continue;
00298         }
00299         // to protect page, use request time stamp
00300         // page->timestamp = param->ctrl->getTimeStamp();
00301         param->pageList->addPage(page);
00302 
00303         // send a message to the main window
00304         postEvent(param, page);
00305     }
00306     return NULL;
00307 }

Here is the call graph for this function:

GBool CRenderThread::abortCheckFunc ( void *  data  )  [static, private]

Definition at line 56 of file RenderThread.cpp.

00057 {
00058     if (abortState == ToAbort)
00059     {
00060         // PV_LOGPRINTF("Abort page!");
00061         return gTrue;
00062     }
00063     return gFalse;
00064     
00065 }

void CRenderThread::start ( ThreadParam param  ) 

Definition at line 77 of file RenderThread.cpp.

00078 {
00079     if (bRunning) 
00080     {
00081         PV_ERRORPRINTF("Thread already running!");
00082         return;
00083     }
00084     param = p;
00085     param->bQuit = gFalse;
00086     param->thread = this;
00087     renderingPage = NULL;
00088     abortState = Initialized;
00089     if (pthread_create(&thread, NULL, threadProc, p) != 0)
00090     {
00091         PV_ERRORPRINTF("Could not create thread!");
00092         return;
00093     }
00094     PV_LOGPRINTF("Create render thread done!");
00095     bRunning = gTrue;
00096 }

Here is the call graph for this function:

void CRenderThread::stop (  ) 

Definition at line 98 of file RenderThread.cpp.

00099 {
00100     if (!bRunning) return;
00101 
00102     abortState = Initialized;
00103     param->bQuit = gTrue;
00104     pthread_mutex_lock(&mutex);
00105     pthread_cond_signal(&cond);
00106     pthread_mutex_unlock(&mutex);
00107     pthread_join(thread, NULL);
00108     bRunning = gFalse;
00109     renderingPage = NULL;
00110 }

void CRenderThread::clearTodoList (  ) 

Definition at line 112 of file RenderThread.cpp.

00113 {
00114     param->todoList->clear();
00115 }

Here is the call graph for this function:

void CRenderThread::addTodoItem ( TodoItem item,
GBool  bHead = gTrue 
)

Definition at line 117 of file RenderThread.cpp.

00118 {
00119     param->todoList->pushItem(item, bHead);
00120 }

Here is the call graph for this function:

void CRenderThread::signal (  ) 

Definition at line 122 of file RenderThread.cpp.

00123 {
00124     pthread_mutex_lock(&mutex);
00125     pthread_cond_signal(&cond);
00126     pthread_mutex_unlock(&mutex);
00127 }

void CRenderThread::renderPageNow ( TodoItem item  ) 

Definition at line 155 of file RenderThread.cpp.

00156 {
00157     // if it's already rendering ignore the request
00158     pthread_mutex_lock(&abort);
00159     if (abortState == Rendering && 
00160         renderingPage &&
00161         renderingPage->isSame(item->pageNumber, item->zoom, item->rotate))
00162     {
00163         PV_LOGPRINTF("Page %d is in rendering!", item->pageNumber);
00164         delete item;
00165         pthread_mutex_unlock(&abort);
00166         return;
00167     }        
00168 
00169     if (abortState == Rendering)
00170     {
00171         abortState = ToAbort;
00172     }
00173     param->todoList->pushItem(item, gTrue);
00174     signal();
00175     pthread_mutex_unlock(&abort);
00176 }

Here is the call graph for this function:

GBool CRenderThread::isInRendering ( TodoItem item  ) 

Definition at line 178 of file RenderThread.cpp.

00179 {
00180     pthread_mutex_lock(&abort);
00181     if (abortState == Rendering && 
00182         renderingPage &&
00183         renderingPage->isSame(item->pageNumber, item->zoom, item->rotate))
00184     {
00185         pthread_mutex_unlock(&abort);
00186         return gTrue;
00187     }        
00188     pthread_mutex_unlock(&abort);
00189     return gFalse;
00190 }

Here is the call graph for this function:


Member Data Documentation

pthread_t CRenderThread::thread [private]

Definition at line 51 of file RenderThread.h.

pthread_cond_t CRenderThread::cond [private]

Definition at line 52 of file RenderThread.h.

pthread_mutex_t CRenderThread::mutex [private]

Definition at line 53 of file RenderThread.h.

pthread_mutex_t CRenderThread::abort [private]

Definition at line 54 of file RenderThread.h.

GBool CRenderThread::bRunning [private]

Definition at line 55 of file RenderThread.h.

Definition at line 56 of file RenderThread.h.

Definition at line 57 of file RenderThread.h.

CRenderThread::threadState CRenderThread::abortState = Initialized [static, private]

Definition at line 64 of file RenderThread.h.


The documentation for this class was generated from the following files:

Generated on Wed Feb 4 18:26:31 2009 by  doxygen 1.5.6