r600: *** empty log message ***
[ctsim.git] / src / threadraster.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          threadraster.cpp
5 **   Purpose:       Threaded rasterizer class
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  February 2001
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id: threadraster.cpp,v 1.4 2001/03/04 03:14:47 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28
29 #if defined(HAVE_CONFIG_H)
30 #include "config.h"
31 #endif
32
33 #include "wx/wxprec.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/wx.h"
37 #endif
38
39 #include "ct.h"
40 #include "ctsim.h"
41 #include "docs.h"
42 #include "views.h"
43
44
45 #ifdef HAVE_WXTHREADS
46
47
48 #include "threadraster.h"
49 #include "backgroundmgr.h"
50 #include "backgroundsupr.h"
51
52
53 /////////////////////////////////////////////////////////////////////
54 //
55 // Class RasterizerSupervisorThread -- Thread for Background Supervisor
56 //
57 /////////////////////////////////////////////////////////////////////
58
59 RasterizerSupervisorThread::RasterizerSupervisorThread (PhantomFileView* pProjView, int iNX, int iNY, 
60    int iNSample, double dViewRatio, const char* const pszLabel)
61 :   SupervisorThread(), m_pPhantomView(pProjView), m_iNX(iNX), m_iNY(iNY), m_iNSample(iNSample), m_dViewRatio(dViewRatio), m_strLabel(pszLabel)
62 {
63 }
64
65 wxThread::ExitCode
66 RasterizerSupervisorThread::Entry()
67 {
68   RasterizerSupervisor rasterSupervisor (this, m_pPhantomView, m_iNX, m_iNY, m_iNSample, m_dViewRatio, m_strLabel.c_str());
69
70   rasterSupervisor.start();
71   while (! rasterSupervisor.isDone() && ! rasterSupervisor.fail()) {
72     Sleep(100);
73     Yield();
74   }
75   if (rasterSupervisor.fail())
76   {
77     wxString msg ("Error starting Rasterizer supervisor: ");
78     msg += rasterSupervisor.getFailMessage().c_str();
79     msg += "\n";
80     wxCommandEvent eventLog (wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
81     eventLog.SetString( msg );
82     wxPostEvent( theApp->getMainFrame(), eventLog ); // send log event
83   }
84
85   while (! rasterSupervisor.workersDeleted()) {
86     Sleep(50);
87     rasterSupervisor.ProcessPendingEvents();
88   }
89
90   return reinterpret_cast<wxThread::ExitCode>(0);
91 }
92
93 void
94 RasterizerSupervisorThread::OnExit()
95 {
96 }
97
98
99 /////////////////////////////////////////////////////////////////////
100 //
101 // Class RasterizerSupervisor -- A Background Supervisor
102 //
103 /////////////////////////////////////////////////////////////////////
104
105 RasterizerSupervisor::RasterizerSupervisor (SupervisorThread* pThread, PhantomFileView* pPhantomView, int iNX, int iNY, 
106    int iNSample, double dViewRatio, const char* const pszLabel)
107     : m_pPhantomView(pPhantomView), m_pPhantomDoc(pPhantomView->GetDocument()), 
108       m_iNX(iNX), m_iNY(iNY), m_iNSample(iNSample), m_dViewRatio(dViewRatio), m_pszLabel(pszLabel), 
109       BackgroundSupervisor (pThread, pPhantomView->GetFrame(), pPhantomView->GetDocument(), "Rasterizing", iNX)
110 {
111   m_vecpChildImageFiles.reserve (getNumWorkers());
112   for (unsigned int iThread = 0; iThread < getNumWorkers(); iThread++) {
113     m_vecpChildImageFiles[iThread] = new ImageFile;
114   }
115
116
117
118 }
119
120 RasterizerSupervisor::~RasterizerSupervisor()
121 {
122   for (int i = 0; i < getNumWorkers(); i++)
123     delete m_vecpChildImageFiles[i];
124 }
125
126 BackgroundWorkerThread*
127 RasterizerSupervisor::createWorker (int iThread, int iStartUnit, int iNumUnits)
128 {
129    RasterizerWorker* pThread = new RasterizerWorker (this, iThread, iStartUnit, iNumUnits);
130    m_vecpChildImageFiles[iThread]->setArraySize (iNumUnits, m_iNY);
131    pThread->SetParameters (m_pPhantomView, m_vecpChildImageFiles[iThread], m_iNX, m_iNY, m_iNSample, m_dViewRatio);
132
133    return pThread;
134 }
135
136 void
137 RasterizerSupervisor::onDone()
138 {
139   wxCriticalSection doneSection;
140   wxCriticalSectionLocker critsect (doneSection);
141
142   ImageFile* pImageFile = getImageFile();
143   pImageFile->labelAdd (m_pszLabel, getTimerEnd());
144   //  *theApp->getLog() << m_pszLabel << "\n";
145
146   wxCommandEvent newImageEvent (wxEVT_COMMAND_MENU_SELECTED, NEW_IMAGEFILE_EVENT);
147   newImageEvent.SetClientData (pImageFile);
148   wxPostEvent (theApp->getMainFrame(), newImageEvent);
149   
150   setDone();
151 }
152
153
154 ImageFile*
155 RasterizerSupervisor::getImageFile()
156 {
157   ImageFile* pImageFile = new ImageFile (m_iNX, m_iNY);
158
159   size_t iColSize = sizeof(ImageFileValue) * m_iNY;
160
161   ImageFileArray globalArray = pImageFile->getArray();
162   int iGlobalCol = 0;
163   for (int iw = 0; iw < getNumWorkers(); iw++) {
164     ImageFileArray childArray = m_vecpChildImageFiles[iw]->getArray();
165     for (int iCol = 0; iCol < m_vecpChildImageFiles[iw]->nx(); iCol++) {
166       memcpy (globalArray[iGlobalCol++], childArray[iCol], iColSize);
167     }
168   }
169   return (pImageFile);
170 }
171
172
173 /////////////////////////////////////////////////////////////////////
174 //
175 // Class RasterizerWorker -- A worker thread
176 //
177 /////////////////////////////////////////////////////////////////////
178
179 void
180 RasterizerWorker::SetParameters (PhantomFileView* pPhantomView, ImageFile* pImageFile, int iNX, int iNY, int iNSample, double dViewRatio)
181 {
182   m_pImageFile = pImageFile;
183   m_iNX = iNX;
184   m_iNY = iNY;
185   m_pPhantomView = pPhantomView;
186   m_iNSample = iNSample;
187   m_dViewRatio = dViewRatio;
188 }
189
190 wxThread::ExitCode
191 RasterizerWorker::Entry ()
192 {
193   const Phantom& rPhantom = m_pPhantomView->GetDocument()->getPhantom();
194   wxCommandEvent eventProgress (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_UNIT_TICK);
195   for (int iUnit = 0; iUnit < m_iNumUnits; iUnit++) {
196     if (TestDestroy()) {
197 #ifdef DEBUG
198       if (theApp->getVerboseLogging()) {
199         wxString msg;
200         msg.Printf("Worker thread: Received destroy message at work unit #%d\n", iUnit);  
201         wxCommandEvent event( wxEVT_COMMAND_MENU_SELECTED, MAINMENU_LOG_EVENT );
202         event.SetString( msg );
203         wxPostEvent( theApp->getMainFrame(), event ); 
204       }
205 #endif
206       break;
207     }
208     rPhantom.convertToImagefile (*m_pImageFile, m_iNX, m_dViewRatio, m_iNSample, Trace::TRACE_NONE, 
209       iUnit + m_iStartUnit, 1, iUnit);
210     wxPostEvent (m_pSupervisor, eventProgress);
211   }
212   wxCommandEvent eventDone (wxEVT_COMMAND_MENU_SELECTED, BackgroundSupervisor::MSG_WORKER_THREAD_DONE);
213   eventDone.SetInt (m_iThread); // Send back thread# that has finished
214   wxPostEvent (m_pSupervisor, eventDone);
215
216   while (! TestDestroy())
217     Sleep(100);
218
219   return reinterpret_cast<wxThread::ExitCode>(0);
220 }
221
222 void
223 RasterizerWorker::OnExit ()
224 {
225 }
226
227 #endif // HAVE_WXTHREADS