- Timestamp:
- Sep 8, 2011, 12:14:54 PM (13 years ago)
- Location:
- branches/vtkvis_threaded
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/vtkvis_threaded/Makefile.in
r2482 r2488 50 50 USE_GPU_RAYCASTING = yes 51 51 USE_OFFSCREEN_RENDERING = yes 52 USE_THREADS = yes 52 53 53 54 #vtk uses deprecated strstream header (instead of sstream) … … 69 70 ifdef USE_GPU_RAYCASTING 70 71 DEFINES += -DUSE_GPU_RAYCAST_MAPPER 72 endif 73 ifdef USE_THREADS 74 DEFINES += -DUSE_THREADS 71 75 endif 72 76 CXX_SWITCHES = $(CXXFLAGS) $(EXTRA_CFLAGS) $(DEFINES) $(INCLUDES) -
branches/vtkvis_threaded/PPMWriter.cpp
r2487 r2488 15 15 #include "Trace.h" 16 16 #include "PPMWriter.h" 17 #ifdef USE_THREADS 17 18 #include "ResponseQueue.h" 19 #endif /*USE_THREADS*/ 18 20 19 21 using namespace Rappture::VtkVis; … … 40 42 * \param[in] height Height of image in pixels 41 43 */ 44 45 #ifdef USE_THREADS 42 46 void 43 Rappture::VtkVis:: writePPM(ResponseQueue *queuePtr, const char *cmdName,47 Rappture::VtkVis::QueuePPM(ResponseQueue *queuePtr, const char *cmdName, 44 48 const unsigned char *data, int width, int height) 45 49 { … … 85 89 86 90 Response *responsePtr; 87 responsePtr = new Response(MESG_IMAGE); 91 if (strncmp(cmdName, "nv>legend", 9) == 0) { 92 responsePtr = new Response(Response::LEGEND); 93 } else { 94 responsePtr = new Response(Response::IMAGE); 95 } 88 96 responsePtr->SetMessage(mesg, length, Response::DYNAMIC); 89 97 queuePtr->Enqueue(responsePtr); 90 98 TRACE("Leaving (%dx%d)\n", width, height); 91 99 } 100 #else 101 102 void 103 Rappture::VtkVis::writePPM(int fd, const char *cmdName, 104 const unsigned char *data, int width, int height) 105 { 106 #define PPM_MAXVAL 255 107 char header[200]; 108 109 TRACE("Entering (%dx%d)\n", width, height); 110 // Generate the PPM binary file header 111 snprintf(header, sizeof(header), "P6 %d %d %d\n", width, height, PPM_MAXVAL); 112 113 size_t headerLength = strlen(header); 114 size_t dataLength = width * height * 3; 115 116 char command[200]; 117 snprintf(command, sizeof(command), "%s %lu\n", cmdName, 118 (unsigned long)headerLength + dataLength); 119 120 size_t nRecs = height + 2; 121 122 struct iovec *iov; 123 iov = (struct iovec *)malloc(sizeof(struct iovec) * nRecs); 124 125 // Write the command, then the image header and data. 126 // Command 127 iov[0].iov_base = command; 128 iov[0].iov_len = strlen(command); 129 // Header of image data 130 iov[1].iov_base = header; 131 iov[1].iov_len = headerLength; 132 133 // Image data 134 size_t bytesPerRow = width * 3; 135 int y; 136 unsigned char *srcRowPtr = const_cast<unsigned char *>(data); 137 for (y = height + 1; y >= 2; y--) { 138 iov[y].iov_base = srcRowPtr; 139 iov[y].iov_len = bytesPerRow; 140 srcRowPtr += bytesPerRow; 141 } 142 if (writev(fd, iov, nRecs) < 0) { 143 ERROR("write failed: %s\n", strerror(errno)); 144 } 145 free(iov); 146 147 TRACE("Leaving (%dx%d)\n", width, height); 148 } 149 150 #endif /*USE_THREADS*/ -
branches/vtkvis_threaded/PPMWriter.h
r2482 r2488 9 9 #define __RAPPTURE_VTKVIS_PPMWRITER_H__ 10 10 11 #ifdef USE_THREADS 11 12 #include "ResponseQueue.h" 13 #endif /*USE_THREADS*/ 12 14 13 15 namespace Rappture { 14 16 namespace VtkVis { 15 extern void writePPM(ResponseQueue *queuePtr, const char *cmdName, 17 #ifdef USE_THREADS 18 extern void QueuePPM(ResponseQueue *queuePtr, const char *cmdName, 16 19 const unsigned char *data, int width, int height); 20 #else 21 extern void writePPM(int fd, const char *cmdName, 22 const unsigned char *data, int width, int height); 23 #endif /*USE_THREADS*/ 17 24 } 18 25 } -
branches/vtkvis_threaded/ResponseQueue.cpp
r2482 r2488 1 2 #ifdef USE_THREADS 1 3 2 4 #include <pthread.h> … … 37 39 INFO("before # of elements is %d\n", _list.size()); 38 40 std::list<Response *>::iterator iter; 39 #ifdef notdef40 41 for (iter = _list.begin(); iter != _list.end(); iter++) { 41 42 /* Remove any responses of the same type. There should be no more than … … 45 46 } 46 47 } 47 #endif48 48 /* Add the new response to the end of the list. */ 49 49 _list.push_back(responsePtr); … … 74 74 return responsePtr; 75 75 } 76 77 #endif /*USE_THREADS*/ -
branches/vtkvis_threaded/ResponseQueue.h
r2487 r2488 9 9 #define _RESPONSE_QUEUE_H 10 10 11 typedef enum ResponseTypes {12 MESG_IMAGE, /* Image to be displayed. */13 MESG_LEGEND, /* Legend to be displayed. */14 MESG_DATA /* Any other type of message. */15 } ResponseType;16 11 17 12 class Response { … … 20 15 STATIC, DYNAMIC, VOLATILE 21 16 } AllocationType; 17 typedef enum ResponseTypes { 18 IMAGE, /* Image to be displayed. */ 19 LEGEND, /* Legend to be displayed. */ 20 DATA /* Any other type of message. */ 21 } ResponseType; 22 22 Response(ResponseType type) { 23 23 _length = 0; -
branches/vtkvis_threaded/RpVtkRenderServer.cpp
r2482 r2488 1 1 2 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 3 /* … … 13 14 #include <unistd.h> 14 15 #include <signal.h> 15 #include <pthread.h>16 16 #include <errno.h> 17 17 … … 26 26 #include "PPMWriter.h" 27 27 #include "TGAWriter.h" 28 #ifdef USE_THREADS 29 #include <pthread.h> 28 30 #include "ResponseQueue.h" 31 #endif /*USE_THREADS*/ 29 32 30 33 using namespace Rappture::VtkVis; … … 41 44 (((t2).tv_sec - (t1).tv_sec))*1.0e+3f + (float)((t2).tv_usec - (t1).tv_usec)/1.0e+3f) 42 45 46 #ifdef USE_THREADS 43 47 static void 44 writeFrame(ResponseQueue *queuePtr, vtkUnsignedCharArray *imgData)48 QueueFrame(ResponseQueue *queuePtr, vtkUnsignedCharArray *imgData) 45 49 { 46 50 #ifdef DEBUG … … 68 72 TARGA_BYTES_PER_PIXEL, 69 73 true); 70 #endif 74 #endif /*RENDER_TARGA*/ 71 75 72 76 #else … … 85 89 86 90 #ifdef RENDER_TARGA 87 writeTGA(queuePtr, oss.str().c_str(),88 imgData->GetPointer(0), 89 g_renderer->getWindowWidth(), 90 g_renderer->getWindowHeight(), 91 TARGA_BYTES_PER_PIXEL); 92 #else 93 writePPM(queuePtr, oss.str().c_str(),91 QueueTGA(queuePtr, oss.str().c_str(), 92 imgData->GetPointer(0), 93 g_renderer->getWindowWidth(), 94 g_renderer->getWindowHeight(), 95 TARGA_BYTES_PER_PIXEL); 96 #else 97 QueuePPM(queuePtr, oss.str().c_str(), 94 98 imgData->GetPointer(0), 95 99 g_renderer->getWindowWidth(), 96 100 g_renderer->getWindowHeight()); 97 #endif 101 #endif /*RENDER_TARGA*/ 98 102 } else { 99 103 #ifdef RENDER_TARGA 100 writeTGA(queuePtr, "nv>image -type image -bytes",101 imgData->GetPointer(0), 102 g_renderer->getWindowWidth(), 103 g_renderer->getWindowHeight(), 104 TARGA_BYTES_PER_PIXEL); 105 #else 106 writePPM(queuePtr, "nv>image -type image -bytes",104 QueueTGA(queuePtr, "nv>image -type image -bytes", 105 imgData->GetPointer(0), 106 g_renderer->getWindowWidth(), 107 g_renderer->getWindowHeight(), 108 TARGA_BYTES_PER_PIXEL); 109 #else 110 QueuePPM(queuePtr, "nv>image -type image -bytes", 107 111 imgData->GetPointer(0), 108 112 g_renderer->getWindowWidth(), 109 113 g_renderer->getWindowHeight()); 110 #endif 111 } 112 #endif 113 } 114 #endif /*RENDER_TARGA*/ 115 } 116 #endif /*DEBUG*/ 117 } 118 119 #else 120 121 static void 122 writeFrame(int fd, vtkUnsignedCharArray *imgData) 123 { 124 #ifdef DEBUG 125 if (g_renderer->getCameraMode() == Renderer::IMAGE) { 126 double xywh[4]; 127 g_renderer->getScreenWorldCoords(xywh); 128 TRACE("Image bbox: %g %g %g %g", 129 xywh[0], 130 (xywh[1] + xywh[3]), 131 (xywh[0] + xywh[2]), 132 xywh[1]); 133 } 134 135 #ifdef RENDER_TARGA 136 writeTGAFile("/tmp/frame.tga", 137 imgData->GetPointer(0), 138 g_renderer->getWindowWidth(), 139 g_renderer->getWindowHeight(), 140 TARGA_BYTES_PER_PIXEL); 141 #else 142 writeTGAFile("/tmp/frame.tga", 143 imgData->GetPointer(0), 144 g_renderer->getWindowWidth(), 145 g_renderer->getWindowHeight(), 146 TARGA_BYTES_PER_PIXEL, 147 true); 148 #endif /*RENDER_TARGA*/ 149 150 #else 151 if (g_renderer->getCameraMode() == Renderer::IMAGE) { 152 double xywh[4]; 153 g_renderer->getCameraZoomRegion(xywh); 154 std::ostringstream oss; 155 oss.precision(12); 156 // Send upper left and lower right corners as bbox 157 oss << "nv>image -type image -bbox {" 158 << std::scientific 159 << xywh[0] << " " 160 << xywh[1] << " " 161 << xywh[2] << " " 162 << xywh[3] << "} -bytes"; 163 164 #ifdef RENDER_TARGA 165 writeTGA(fd, oss.str().c_str(), 166 imgData->GetPointer(0), 167 g_renderer->getWindowWidth(), 168 g_renderer->getWindowHeight(), 169 TARGA_BYTES_PER_PIXEL); 170 #else 171 writePPM(fd, oss.str().c_str(), 172 imgData->GetPointer(0), 173 g_renderer->getWindowWidth(), 174 g_renderer->getWindowHeight()); 175 #endif /*RENDER_TARGA*/ 176 } else { 177 #ifdef RENDER_TARGA 178 writeTGA(fd, "nv>image -type image -bytes", 179 imgData->GetPointer(0), 180 g_renderer->getWindowWidth(), 181 g_renderer->getWindowHeight(), 182 TARGA_BYTES_PER_PIXEL); 183 #else 184 writePPM(fd, "nv>image -type image -bytes", 185 imgData->GetPointer(0), 186 g_renderer->getWindowWidth(), 187 g_renderer->getWindowHeight()); 188 #endif /*RENDER_TARGA*/ 189 } 190 #endif /*DEBUG*/ 191 } 192 #endif /*USE_THREADS*/ 114 193 115 194 static void … … 156 235 } 157 236 237 #ifdef USE_THREADS 158 238 static void * 159 239 ReaderThread(void *clientData) … … 175 255 TRACE("Rendering new frame"); 176 256 g_renderer->getRenderedFrame(imgData); 177 writeFrame(queuePtr, imgData);257 QueueFrame(queuePtr, imgData); 178 258 } else { 179 259 TRACE("No render required"); … … 208 288 } 209 289 290 #endif /*USE_THREADS*/ 291 210 292 int 211 293 main(int argc, char *argv[]) … … 233 315 234 316 Tcl_Interp *interp = Tcl_CreateInterp(); 317 318 #ifdef USE_THREADS 235 319 ResponseQueue queue((void *)interp); 236 320 initTcl(interp, (ClientData)&queue); … … 249 333 ERROR("can't join writer thread: %s", strerror(errno)); 250 334 } 335 #else 336 initTcl(interp, (ClientData)NULL); 337 int ret = 0; 338 339 vtkSmartPointer<vtkUnsignedCharArray> imgData = 340 vtkSmartPointer<vtkUnsignedCharArray>::New(); 341 while (1) { 342 ret = processCommands(interp, g_fIn, g_fOut); 343 if (ret < 0) 344 break; 345 346 if (g_renderer->render()) { 347 TRACE("Rendering new frame"); 348 g_renderer->getRenderedFrame(imgData); 349 writeFrame(g_fdOut, imgData); 350 } else { 351 TRACE("No render required"); 352 } 353 354 if (feof(g_fIn)) 355 break; 356 } 357 #endif 251 358 INFO("exiting vtkvis"); 252 359 exitTcl(interp); -
branches/vtkvis_threaded/RpVtkRendererCmd.cpp
r2487 r2488 23 23 #include "PPMWriter.h" 24 24 #include "TGAWriter.h" 25 #ifdef USE_THREADS 25 26 #include "ResponseQueue.h" 27 #endif /*USE_THREADS*/ 26 28 27 29 using namespace Rappture::VtkVis; … … 29 31 static int lastCmdStatus; 30 32 33 #ifdef USE_THREADS 31 34 static void 32 35 QueueResponse(ClientData clientData, const void *bytes, size_t len, … … 37 40 Response *responsePtr; 38 41 39 responsePtr = new Response( MESG_DATA);42 responsePtr = new Response(Response::DATA); 40 43 responsePtr->SetMessage((unsigned char *)bytes, len, allocType); 41 44 queuePtr->Enqueue(responsePtr); 42 45 } 46 #else 43 47 44 48 static ssize_t … … 57 61 return bytesWritten; 58 62 } 63 64 #endif /*USE_THREADS*/ 59 65 60 66 static int … … 368 374 pos[0], pos[1], pos[2], focalPt[0], focalPt[1], focalPt[2], viewUp[0], viewUp[1], viewUp[2]); 369 375 376 #ifdef USE_THREADS 370 377 QueueResponse(clientData, buf, strlen(buf), Response::VOLATILE); 378 #else 379 ssize_t bytesWritten = SocketWrite(buf, strlen(buf)); 380 if (bytesWritten < 0) { 381 return TCL_ERROR; 382 } 383 #endif 371 384 return TCL_OK; 372 385 } … … 1369 1382 x, y, value, name); 1370 1383 1384 #ifdef USE_THREADS 1371 1385 QueueResponse(clientData, buf, length, Response::VOLATILE); 1386 #else 1387 ssize_t bytesWritten = SocketWrite(buf, length); 1388 1389 if (bytesWritten < 0) { 1390 return TCL_ERROR; 1391 } 1392 #endif 1372 1393 return TCL_OK; 1373 1394 } … … 1396 1417 "nv>dataset scalar world %g %g %g %g %s\n", x, y, z, value, name); 1397 1418 1419 #ifdef USE_THREADS 1398 1420 QueueResponse(clientData, buf, length, Response::VOLATILE); 1421 #else 1422 ssize_t bytesWritten = SocketWrite(buf, length); 1423 if (bytesWritten < 0) { 1424 return TCL_ERROR; 1425 } 1426 #endif 1399 1427 return TCL_OK; 1400 1428 } … … 1442 1470 value[1], value[2], name); 1443 1471 1472 #ifdef USE_THREADS 1444 1473 QueueResponse(clientData, buf, length, Response::VOLATILE); 1474 #else 1475 ssize_t bytesWritten = SocketWrite(buf, length); 1476 1477 if (bytesWritten < 0) { 1478 return TCL_ERROR; 1479 } 1480 #endif /*USE_THREADS*/ 1445 1481 return TCL_OK; 1446 1482 } … … 1468 1504 "nv>dataset vector world %g %g %g %g %g %g %s\n", x, y, z, 1469 1505 value[0], value[1], value[2], name); 1506 #ifdef USE_THREADS 1470 1507 QueueResponse(clientData, buf, length, Response::VOLATILE); 1508 #else 1509 ssize_t bytesWritten = SocketWrite(buf, length); 1510 1511 if (bytesWritten < 0) { 1512 return TCL_ERROR; 1513 } 1514 #endif /*USE_THREADS*/ 1471 1515 return TCL_OK; 1472 1516 } … … 1531 1575 oss << "}\n"; 1532 1576 len += 2; 1577 #ifdef USE_THREADS 1533 1578 QueueResponse(clientData, oss.str().c_str(), len, Response::VOLATILE); 1579 #else 1580 ssize_t bytesWritten = SocketWrite(oss.str().c_str(), len); 1581 1582 if (bytesWritten < 0) { 1583 return TCL_ERROR; 1584 } 1585 #endif /*USE_THREADS*/ 1534 1586 return TCL_OK; 1535 1587 } … … 2635 2687 TARGA_BYTES_PER_PIXEL); 2636 2688 #else 2689 #ifdef USE_THREADS 2637 2690 ResponseQueue *queuePtr = (ResponseQueue *)clientData; 2638 writePPM(queuePtr, cmd, imgData->GetPointer(0), width, height); 2639 #endif 2640 #endif 2641 2691 QueuePPM(queuePtr, cmd, imgData->GetPointer(0), width, height); 2692 #else 2693 writePPM(g_fdOut, cmd, imgData->GetPointer(0), width, height); 2694 #endif /*USE_THREADS*/ 2695 #endif /*RENDER_TARGA*/ 2696 #endif /*DEBUG*/ 2642 2697 return TCL_OK; 2643 2698 } -
branches/vtkvis_threaded/TGAWriter.cpp
r2260 r2488 17 17 #include "TGAWriter.h" 18 18 #include "Trace.h" 19 #ifdef USE_THREADS 20 #include "ResponseQueue.h" 21 #endif /*USE_THREADS*/ 19 22 20 23 /** … … 31 34 * \param[in] bytesPerPixel Should be 3 or 4, depending on alpha 32 35 */ 36 #ifdef USE_THREADS 37 void 38 Rappture::VtkVis::QueueTGA(ResponseQueue *queuePtr, const char *cmdName, 39 const unsigned char *data, 40 int width, int height, 41 int bytesPerPixel) 42 { 43 TRACE("(%dx%d)\n", width, height); 44 45 size_t headerLength = 18; 46 47 char header[headerLength]; 48 memset(header, 0, headerLength); 49 header[2] = (char)2; // image type (2 = uncompressed true-color) 50 header[12] = (char)width; 51 header[13] = (char)(width >> 8); 52 header[14] = (char)height; 53 header[15] = (char)(height >> 8); 54 header[16] = (char)(bytesPerPixel*8); // bits per pixel 55 56 size_t dataLength = width * height * bytesPerPixel; 57 size_t cmdLength; 58 59 char command[200]; 60 cmdLength = snprintf(command, sizeof(command), "%s %lu\n", cmdName, 61 (unsigned long)headerLength + dataLength); 62 63 size_t length; 64 unsigned char *mesg; 65 66 length = headerLength + dataLength + cmdLength; 67 mesg = (unsigned char *)malloc(length); 68 if (mesg == NULL) { 69 ERROR("can't allocate %ld bytes for the image message", length); 70 return; 71 } 72 memcpy(mesg, command, cmdLength); 73 memcpy(mesg + cmdLength, header, headerLength); 74 75 size_t bytesPerRow = width * 3; 76 unsigned char *destRowPtr = mesg + cmdLength + headerLength; 77 int y; 78 unsigned char *srcRowPtr = const_cast<unsigned char *>(data); 79 for (y = 0; y < height; y++) { 80 memcpy(destRowPtr, srcRowPtr, bytesPerRow); 81 srcRowPtr += bytesPerRow; 82 destRowPtr += bytesPerRow; 83 } 84 85 Response *responsePtr; 86 if (strncmp(cmdName, "nv>legend", 9) == 0) { 87 responsePtr = new Response(Response::LEGEND); 88 } else { 89 responsePtr = new Response(Response::IMAGE); 90 } 91 responsePtr->SetMessage(mesg, length, Response::DYNAMIC); 92 queuePtr->Enqueue(responsePtr); 93 TRACE("Leaving (%dx%d)\n", width, height); 94 } 95 #else 96 33 97 void 34 98 Rappture::VtkVis::writeTGA(int fd, const char *cmdName, … … 79 143 TRACE("Leaving (%dx%d)\n", width, height); 80 144 } 145 #endif /*USE_THREADS*/ 81 146 82 147 /** -
branches/vtkvis_threaded/TGAWriter.h
r2260 r2488 9 9 #define __RAPPTURE_VTKVIS_TGAWRITER_H__ 10 10 11 #ifdef USE_THREADS 12 #include "ResponseQueue.h" 13 #endif /*USE_THREADS*/ 14 11 15 namespace Rappture { 12 namespace VtkVis { 13 14 extern 15 void writeTGA(int fd, const char *cmdName, const unsigned char *data, 16 int width, int height, int bytesPerPixel); 17 18 extern 19 void writeTGAFile(const char *filename, const unsigned char *data, 20 int width, int height, int bytesPerPixel, 21 bool srcIsRGB = false); 22 23 } 16 namespace VtkVis { 17 #ifdef USE_THREADS 18 extern void QueueTGA(ResponseQueue *queuePtr, const char *cmdName, 19 const unsigned char *data, int width, int height, 20 int bytesPerPixel); 21 #else 22 extern void writeTGA(int fd, const char *cmdName, 23 const unsigned char *data, int width, int height, 24 int bytesPerPixel); 25 #endif /*USE_THREADS*/ 26 extern void writeTGAFile(const char *filename, 27 const unsigned char *data, int width, int height, 28 int bytesPerPixel, bool srcIsRGB = false); 29 } 24 30 } 25 31 26 #endif 32 #endif /*__RAPPTURE_VTKVIS_TGA_WRITER_H__*/
Note: See TracChangeset
for help on using the changeset viewer.