Changeset 2488 for branches


Ignore:
Timestamp:
Sep 8, 2011, 12:14:54 PM (13 years ago)
Author:
gah
Message:

added ifdefs for threaded compilation

Location:
branches/vtkvis_threaded
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • branches/vtkvis_threaded/Makefile.in

    r2482 r2488  
    5050USE_GPU_RAYCASTING      = yes
    5151USE_OFFSCREEN_RENDERING = yes
     52USE_THREADS             = yes
    5253
    5354#vtk uses deprecated strstream header (instead of sstream)
     
    6970ifdef USE_GPU_RAYCASTING
    7071DEFINES         += -DUSE_GPU_RAYCAST_MAPPER
     72endif
     73ifdef USE_THREADS
     74DEFINES         += -DUSE_THREADS
    7175endif
    7276CXX_SWITCHES    = $(CXXFLAGS) $(EXTRA_CFLAGS) $(DEFINES) $(INCLUDES)
  • branches/vtkvis_threaded/PPMWriter.cpp

    r2487 r2488  
    1515#include "Trace.h"
    1616#include "PPMWriter.h"
     17#ifdef USE_THREADS
    1718#include "ResponseQueue.h"
     19#endif  /*USE_THREADS*/
    1820
    1921using namespace Rappture::VtkVis;
     
    4042 * \param[in] height Height of image in pixels
    4143 */
     44
     45#ifdef USE_THREADS
    4246void
    43 Rappture::VtkVis::writePPM(ResponseQueue *queuePtr, const char *cmdName,
     47Rappture::VtkVis::QueuePPM(ResponseQueue *queuePtr, const char *cmdName,
    4448                           const unsigned char *data, int width, int height)
    4549{
     
    8589
    8690    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    }
    8896    responsePtr->SetMessage(mesg, length, Response::DYNAMIC);
    8997    queuePtr->Enqueue(responsePtr);
    9098    TRACE("Leaving (%dx%d)\n", width, height);
    9199}
     100#else
     101
     102void
     103Rappture::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  
    99#define __RAPPTURE_VTKVIS_PPMWRITER_H__
    1010
     11#ifdef USE_THREADS
    1112#include "ResponseQueue.h"
     13#endif /*USE_THREADS*/
    1214
    1315namespace Rappture {
    1416    namespace VtkVis {
    15         extern void writePPM(ResponseQueue *queuePtr, const char *cmdName,
     17#ifdef USE_THREADS
     18        extern void QueuePPM(ResponseQueue *queuePtr, const char *cmdName,
    1619                             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*/
    1724    }
    1825}
  • branches/vtkvis_threaded/ResponseQueue.cpp

    r2482 r2488  
     1
     2#ifdef USE_THREADS
    13
    24#include <pthread.h>
     
    3739    INFO("before # of elements is %d\n", _list.size());
    3840    std::list<Response *>::iterator iter;
    39 #ifdef notdef
    4041    for (iter = _list.begin(); iter != _list.end(); iter++) {
    4142        /* Remove any responses of the same type. There should be no more than
     
    4546        }
    4647    }
    47 #endif
    4848    /* Add the new response to the end of the list. */
    4949    _list.push_back(responsePtr);
     
    7474    return responsePtr;
    7575}
     76
     77#endif /*USE_THREADS*/
  • branches/vtkvis_threaded/ResponseQueue.h

    r2487 r2488  
    99#define _RESPONSE_QUEUE_H
    1010
    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;
    1611
    1712class Response {
     
    2015        STATIC, DYNAMIC, VOLATILE
    2116    } 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;
    2222    Response(ResponseType type) {
    2323        _length = 0;
  • branches/vtkvis_threaded/RpVtkRenderServer.cpp

    r2482 r2488  
     1
    12/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
    23/*
     
    1314#include <unistd.h>
    1415#include <signal.h>
    15 #include <pthread.h>
    1616#include <errno.h>
    1717
     
    2626#include "PPMWriter.h"
    2727#include "TGAWriter.h"
     28#ifdef USE_THREADS
     29#include <pthread.h>
    2830#include "ResponseQueue.h"
     31#endif  /*USE_THREADS*/
    2932
    3033using namespace Rappture::VtkVis;
     
    4144     (((t2).tv_sec - (t1).tv_sec))*1.0e+3f + (float)((t2).tv_usec - (t1).tv_usec)/1.0e+3f)
    4245
     46#ifdef USE_THREADS
    4347static void
    44 writeFrame(ResponseQueue *queuePtr, vtkUnsignedCharArray *imgData)
     48QueueFrame(ResponseQueue *queuePtr, vtkUnsignedCharArray *imgData)
    4549{
    4650#ifdef DEBUG
     
    6872                 TARGA_BYTES_PER_PIXEL,
    6973                 true);
    70 #endif
     74#endif  /*RENDER_TARGA*/
    7175
    7276#else
     
    8589
    8690#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(),
    9498                 imgData->GetPointer(0),
    9599                 g_renderer->getWindowWidth(),
    96100                 g_renderer->getWindowHeight());
    97 #endif
     101#endif  /*RENDER_TARGA*/
    98102    } else {
    99103#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",
    107111                 imgData->GetPointer(0),
    108112                 g_renderer->getWindowWidth(),
    109113                 g_renderer->getWindowHeight());
    110 #endif
    111     }
    112 #endif
    113 }
     114#endif  /*RENDER_TARGA*/
     115    }
     116#endif  /*DEBUG*/
     117}
     118
     119#else
     120
     121static void
     122writeFrame(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*/
    114193
    115194static void
     
    156235}
    157236
     237#ifdef USE_THREADS
    158238static void *
    159239ReaderThread(void *clientData)
     
    175255            TRACE("Rendering new frame");
    176256            g_renderer->getRenderedFrame(imgData);
    177             writeFrame(queuePtr, imgData);
     257            QueueFrame(queuePtr, imgData);
    178258        } else {
    179259            TRACE("No render required");
     
    208288}
    209289
     290#endif  /*USE_THREADS*/
     291
    210292int
    211293main(int argc, char *argv[])
     
    233315
    234316    Tcl_Interp *interp = Tcl_CreateInterp();
     317
     318#ifdef USE_THREADS
    235319    ResponseQueue queue((void *)interp);
    236320    initTcl(interp, (ClientData)&queue);
     
    249333        ERROR("can't join writer thread: %s", strerror(errno));
    250334    }
     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
    251358    INFO("exiting vtkvis");
    252359    exitTcl(interp);
  • branches/vtkvis_threaded/RpVtkRendererCmd.cpp

    r2487 r2488  
    2323#include "PPMWriter.h"
    2424#include "TGAWriter.h"
     25#ifdef USE_THREADS
    2526#include "ResponseQueue.h"
     27#endif  /*USE_THREADS*/
    2628
    2729using namespace Rappture::VtkVis;
     
    2931static int lastCmdStatus;
    3032
     33#ifdef USE_THREADS
    3134static void
    3235QueueResponse(ClientData clientData, const void *bytes, size_t len,
     
    3740    Response *responsePtr;
    3841   
    39     responsePtr = new Response(MESG_DATA);
     42    responsePtr = new Response(Response::DATA);
    4043    responsePtr->SetMessage((unsigned char *)bytes, len, allocType);
    4144    queuePtr->Enqueue(responsePtr);
    4245}
     46#else
    4347
    4448static ssize_t
     
    5761    return bytesWritten;
    5862}
     63
     64#endif  /*USE_THREADS*/
    5965
    6066static int
     
    368374             pos[0], pos[1], pos[2], focalPt[0], focalPt[1], focalPt[2], viewUp[0], viewUp[1], viewUp[2]);
    369375
     376#ifdef USE_THREADS
    370377    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
    371384    return TCL_OK;
    372385}
     
    13691382        x, y, value, name);
    13701383
     1384#ifdef USE_THREADS
    13711385    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
    13721393    return TCL_OK;
    13731394}
     
    13961417        "nv>dataset scalar world %g %g %g %g %s\n", x, y, z, value, name);
    13971418
     1419#ifdef USE_THREADS
    13981420    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
    13991427    return TCL_OK;
    14001428}
     
    14421470        value[1], value[2], name);
    14431471
     1472#ifdef USE_THREADS
    14441473    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*/
    14451481    return TCL_OK;
    14461482}
     
    14681504        "nv>dataset vector world %g %g %g %g %g %g %s\n", x, y, z,
    14691505        value[0], value[1], value[2], name);
     1506#ifdef USE_THREADS
    14701507    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*/
    14711515    return TCL_OK;
    14721516}
     
    15311575    oss << "}\n";
    15321576    len += 2;
     1577#ifdef USE_THREADS
    15331578    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*/
    15341586    return TCL_OK;
    15351587}
     
    26352687                 TARGA_BYTES_PER_PIXEL);
    26362688#else
     2689#ifdef USE_THREADS
    26372690    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*/
    26422697    return TCL_OK;
    26432698}
  • branches/vtkvis_threaded/TGAWriter.cpp

    r2260 r2488  
    1717#include "TGAWriter.h"
    1818#include "Trace.h"
     19#ifdef USE_THREADS
     20#include "ResponseQueue.h"
     21#endif  /*USE_THREADS*/
    1922
    2023/**
     
    3134 * \param[in] bytesPerPixel Should be 3 or 4, depending on alpha
    3235 */
     36#ifdef USE_THREADS
     37void
     38Rappture::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
    3397void
    3498Rappture::VtkVis::writeTGA(int fd, const char *cmdName,
     
    79143    TRACE("Leaving (%dx%d)\n", width, height);
    80144}
     145#endif  /*USE_THREADS*/
    81146
    82147/**
  • branches/vtkvis_threaded/TGAWriter.h

    r2260 r2488  
    99#define __RAPPTURE_VTKVIS_TGAWRITER_H__
    1010
     11#ifdef USE_THREADS
     12#include "ResponseQueue.h"
     13#endif /*USE_THREADS*/
     14
    1115namespace 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    }
    2430}
    2531
    26 #endif
     32#endif  /*__RAPPTURE_VTKVIS_TGA_WRITER_H__*/
Note: See TracChangeset for help on using the changeset viewer.