Commit 1d3dda3b authored by fogal1's avatar fogal1
Browse files

Add new classes to abstract display setup.

VisItDisplay is an interface for starting up and tearing down rendering
contexts.  In the case of an XDisplay, it creates an X-based hardware
rendering context.  A MesaDisplay is a lot of no-ops, with a small bit
of code which delegates to InitVTK.

git-svn-id: http://visit.ilight.com/svn/visit/trunk/src@5119 18c085ea-50e0-402c-830e-de6fd14e8384
parent b6c3dceb
......@@ -270,6 +270,9 @@
# Hank Childs Tue Aug 19 21:57:35 PDT 2008
# Add cognomen to the purify line.
#
# Tom Fogal, Fri Aug 29 20:06:37 EDT 2008
# Add new src files for abstracted rendering contexts. Vertical formatting.
#
##############################################################################
##
......@@ -324,13 +327,16 @@ PARLIBDEP=../../lib/libplugin.@SHLIB_LIBEXT@ ../../lib/libplotter_par.@SHLIB_LIB
##
## Files...
##
SRC= \
DataNetwork.C \
SRC= \
DataNetwork.C \
ClonedDataNetwork.C \
Engine.C \
LoadBalancer.C \
Netnodes.C \
NetworkManager.C
Engine.C \
LoadBalancer.C \
MesaDisplay.C \
Netnodes.C \
NetworkManager.C \
VisItDisplay.C \
XDisplay.C
MAINSRC=main.C
......
/*****************************************************************************
*
* Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400142
* All rights reserved.
*
* This file is part of VisIt. For details, see https://visit.llnl.gov/. The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer (as noted below) in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#include <cerrno>
#include <MesaDisplay.h>
#include <DebugStream.h>
#include <InitVTK.h>
// ****************************************************************************
// Method: MesaDisplay constructor
//
// Purpose:
// Sets the default state for a Mesa display; a no-op, we have no state.
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// ****************************************************************************
MesaDisplay::MesaDisplay() { }
// ****************************************************************************
// Method: MesaDisplay destructor
//
// Purpose:
// Ensures we've disassociated ourselves from any Mesa context.
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// ****************************************************************************
MesaDisplay::~MesaDisplay()
{
// A MesaDisplay has no state, and thus we can safely always consider it
// initialized. This might need to be revisited should Teardown's
// implementation change from a no-op.
this->Teardown();
}
// ****************************************************************************
// Method: MesaDisplay::Initialize
//
// Purpose:
// No-op; Mesa only does something at the `connect' stage.
//
// Returns: success, always.
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// ****************************************************************************
bool
MesaDisplay::Initialize(size_t display, const std::vector<std::string> &user_args)
{
return true;
}
// ****************************************************************************
// Method: MesaDisplay::Connect
//
// Purpose:
// Associates this process with an X server.
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// Modifications:
//
// Brad Whitlock, Fri Aug 29 09:55:09 PDT 2008
// Added Mac-specific code for unsetenv since it returns void on Mac.
//
// ****************************************************************************
void
MesaDisplay::Connect()
{
InitVTK::ForceMesa();
#ifdef __APPLE__
unsetenv("DISPLAY");
#else
if(unsetenv("DISPLAY") != 0)
{
char err[1024];
strerror_r(errno, err, 1024);
debug1 << "unsetenv(DISPLAY) failed: " << err << std::endl;
}
#endif
}
// ****************************************************************************
// Method: MesaDisplay::Teardown
//
// Purpose:
// No-op; there's really no way to `disassociate' from a Mesa context in
// VTK/VisIt
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// ****************************************************************************
void
MesaDisplay::Teardown()
{
}
/*****************************************************************************
*
* Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400142
* All rights reserved.
*
* This file is part of VisIt. For details, see https://visit.llnl.gov/. The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer (as noted below) in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#ifndef VISIT_MESA_DISPLAY_H
#define VISIT_MESA_DISPLAY_H
#include <VisItDisplay.h>
// ****************************************************************************
// Class: MesaDisplay
//
// Purpose:
// Initializes a Mesa display; mostly delegates to InitVTK.
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// ****************************************************************************
class MesaDisplay : public VisItDisplay
{
public:
MesaDisplay();
virtual ~MesaDisplay();
virtual bool Initialize(size_t n,
const std::vector<std::string> &args);
virtual void Connect();
virtual void Teardown();
};
#endif /* VISIT_MESA_DISPLAY_H */
/*****************************************************************************
*
* Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400142
* All rights reserved.
*
* This file is part of VisIt. For details, see https://visit.llnl.gov/. The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer (as noted below) in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#include <string>
#include <vector>
#include <VisItDisplay.h>
#include <DebugStream.h>
#include <snprintf.h>
#include <StringHelpers.h>
// ****************************************************************************
// Function: format
//
// Purpose: Replace special formatters within a string with particular values.
// Currently `%l' expands to the display, and `%n' expands to the
// node ID.
//
// Programmer: Tom Fogal
// Creation: August 5, 2008
//
// Modifications:
//
// Tom Fogal, Thu Aug 7 16:43:49 EDT 2008
// Use a debug stream instead of cerr. Use SNPRINTF macro instead of
// calling the function directly.
//
// ****************************************************************************
std::string
format(std::string s, size_t node, size_t display)
{
std::string::size_type percent;
if((percent = s.find('%')) != std::string::npos) {
std::string::iterator start = s.begin() + percent;
/* assume all formatters are '%x': i.e., always 2 characters */
std::string::iterator end = s.begin() + (percent+2);
std::string::iterator type = s.begin() + (percent+1);
if(*type == 'l') {
char str_display[8];
SNPRINTF(str_display, 8, "%zu", display);
s.replace(start, end, str_display);
} else if(*type == 'n') {
char str_node[8];
SNPRINTF(str_node, 8, "%zu", node);
s.replace(start, end, str_node);
} else {
debug5 << "unknown formatter '" << *type << "'" << std::endl;
}
}
// If they gave multiple %'s in the same string, recurse.
if(s.find('%') != std::string::npos) {
return format(s, node, display);
}
return s;
}
// ****************************************************************************
// Function: split
//
// Purpose: Splits a string into a vector of strings, separated by spaces.
// Converts node and display IDs (via format specifiers) as it
// proceeds.
//
// Programmer: Tom Fogal
// Creation: August 5, 2008
//
// ****************************************************************************
std::vector<std::string>
split(std::string str, size_t node, size_t display)
{
namespace SH = StringHelpers;
std::vector<std::string> ret;
ret.push_back(format(SH::car(str), node, display));
if(str.find(' ') != std::string::npos) {
SH::append(ret, split(SH::cdr(str), node, display));
}
return ret;
}
/*****************************************************************************
*
* Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400142
* All rights reserved.
*
* This file is part of VisIt. For details, see https://visit.llnl.gov/. The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer (as noted below) in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#ifndef VISIT_VISIT_DISPLAY_H
#define VISIT_VISIT_DISPLAY_H
#include <string>
#include <vector>
// ****************************************************************************
// Class: VisItDisplay
//
// Purpose:
// Interface for initializing a display to utilize in rendering.
//
// Programmer: Tom Fogal
// Creation: September 1, 2008
//
// ****************************************************************************
class VisItDisplay
{
public:
virtual ~VisItDisplay() {}
/// Initializes a VisItDisplay. Rendering is only possible after
/// `Initialize' and before `Teardown'. `Initialize' must be called prior
/// to any rendering commands. Multiple calls to `Initialize' without
/// intervening `Teardown's result in undefined behavior.
/// Initialization may fail; success is given via the return value.
///
/// n The display number (identifier) for this display.
/// args user arguments appropriate for configuring the VisIt display.
/// returns: Display initialization status.
virtual bool Initialize(size_t n,
const std::vector<std::string> &args) = 0;
/// Associates this process with the previously-`Initialize'd VisIt Display.
/// Undefined if this VisItDisplay has not been initialized.
virtual void Connect() = 0;
/// Closes down a VisItDisplay. You may assume that this will be called by
/// the destructor. It is safe to call Teardown more than once. It is not
/// safe to use `Teardown' before `Initialize'. This implicitly breaks any
/// previous connections.
virtual void Teardown() = 0;
};
// Methods helpful in argument vector creation.
// If these were to be generalized, they would more appropriately end up in
// `StringHelpers'
std::string format(std::string s, size_t node, size_t display);
std::vector<std::string> split(std::string, size_t, size_t);
#endif /* VISIT_VISIT_DISPLAY_H */
/*****************************************************************************
*
* Copyright (c) 2000 - 2008, Lawrence Livermore National Security, LLC
* Produced at the Lawrence Livermore National Laboratory
* LLNL-CODE-400142
* All rights reserved.
*
* This file is part of VisIt. For details, see https://visit.llnl.gov/. The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the disclaimer (as noted below) in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the LLNS/LLNL nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY,
* LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#include <cerrno>
#include <cstdlib>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
#include <XDisplay.h>
#include <DebugStream.h>
#include <InitVTK.h>
#include <snprintf.h>
#include <StringHelpers.h>
#ifdef DEBUG_X
# define DEBUG_ONLY(block) block
#else
# define DEBUG_ONLY(block) /* nothing */
#endif
static inline bool initialized(pid_t x) { return ((int)x != -1); }
static inline void set_uninitialized(pid_t *x) { *x = (pid_t) -1; }
static char **vec_convert(std::vector<std::string> svec, size_t *len);
void vec_convert_free(char **vec, size_t len);
// ****************************************************************************
// Method: XDisplay constructor
//
// Purpose:
// Sets the default state of uninitialized X server for display 0.
//
// Programmer: Tom Fogal
// Creation: August 29, 2008
//
// ****************************************************************************
XDisplay::XDisplay(): xserver((pid_t)-1), display(0) { }
// ****************************************************************************
// Method: XDisplay destructor
//
// Purpose:
// Ensures the X server has been brought down.
//
// Programmer: Tom Fogal
// Creation: August 29, 2008
//
// ****************************************************************************
XDisplay::~XDisplay()
{
if(initialized(this->xserver))
{
this->Teardown();
}
}
// ****************************************************************************
// Method: XDisplay::Initialize
//
// Purpose:
// Fork-exec an X server
//
// Returns: Success / failure.
//
// Arguments:
// display Display number to create.
// user_args User-given arguments to xinit.
//
// Programmer: Tom Fogal
// Creation: July 27, 2008
//
// Modifications:
//
// Tom Fogal, Tue Aug 5 16:33:49 EDT 2008
// Add argument string-vector, and code to convert that into an argv-style
// array. Finally, use execvp to start xinit. All of this allows the user
// to specify their own custom arguments to the X launch.
//
// Tom Fogal, Mon Aug 11 19:02:11 EDT 2008
// Move print out of the child; this is confusing for the output stream (two
// processes can write to it, concurrently!). Convert to an argv[] array in
// both processes, so that we can still output the X server options.
//
// ****************************************************************************
bool
XDisplay::Initialize(size_t display, const std::vector<std::string> &user_args)
{
char **argv;
size_t v_elems;
this->display = display;
std::vector<std::string> args;
args.push_back("xinit");
args.push_back("--");
args.push_back(format(":%l", /* unused */0, display));
args.push_back("-sharevts");
args.push_back("-once");
args.push_back("-terminate");
StringHelpers::append(args, user_args);
argv = vec_convert(args, &v_elems);
if((this->xserver = fork()) == (pid_t) -1)
{
perror("fork");
vec_convert_free(argv, v_elems);
return false;
}
if(this->xserver == 0)
{
execvp("xinit", argv);
perror("execvp of xinit");
vec_convert_free(argv, v_elems);
return false;
}
DEBUG_ONLY(
debug5 << "X server command line arguments:" << std::endl;
for(size_t i=0; i < args.size(); ++i)
{
debug5 << "\t" << argv[i] << std::endl;
}
)
vec_convert_free(argv, v_elems);
debug4 << "Giving a sec for the X server to start ...";
sleep(display);
debug4 << " done!" << std::endl;
debug3 << "Saved X server PID " << (int)this->xserver << std::endl;
return true;
}
// ****************************************************************************
// Method: XDisplay::Connect
//
// Purpose:
// Associates this process with an X server.
//
// Programmer: Tom Fogal
// Creation: August 29, 2008 (from a July 27th method)
//
// Modifications:
//
// Tom Fogal, Tue Aug 5 16:36:20 EDT 2008
// Dropped the array size down a notch; why was it so huge before?