PCMGLXGLContext.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMGLXGLContext.h"
00030 
00031 #include "../include/PCMGLXRenderWindow.h"
00032 
00033 #include "../include/PCMStringConverter.h"
00034 
00035 namespace ParCompMark
00036 {
00037 
00038   //
00039   // Constructors & destructor
00040   //
00041 
00042   GLXGLContext::GLXGLContext(XDisplay::Pointer & display, GLXRenderWindow * glxWindow,::XVisualInfo * visualInfo):
00043         // X Display pointer initializer 
00044   mDisplay(display              /* Corresponding X Display */
00045   ),
00046         // Renderwindow initializer 
00047   mGLXWindow(glxWindow  /* Corresponding GLX rendering window. */
00048         )
00049         // You have to initialize the following attributes:
00050         // - mInitialized
00051         // - mDisplay
00052         // - mGLXWindow
00053         // - mVisualInfo
00054         // - mGLXContext
00055         // - mTainted
00056   {
00057         Assert(display.isNotNull() && glxWindow && visualInfo, NULL_POINTER_ERROR, "GLXGLContext::GLXGLContext()");
00058 
00059         mInitialized = false;
00060         mGLXContext = 0;
00061         mTainted = false;
00062 
00063         // Copy visual info
00064         mVisualInfo = new::XVisualInfo();
00065         memcpy(mVisualInfo, visualInfo, sizeof(::XVisualInfo));
00066 
00067         Logger::getInstance()->log(Logger::NOTICE,
00068                                  "GLX Context for window `" + mGLXWindow->getCaption() + "\' has been created");
00069   }
00070 
00071  /*----------------------------------------------------------------------*/
00072 
00073   GLXGLContext::~GLXGLContext()
00074   {
00075         if(mInitialized)
00076          finalize();
00077 
00078         // Delete visual info
00079         Assert(mVisualInfo, NULL_POINTER_ERROR, "GLXGLContext::finalize()");
00080         delete mVisualInfo;
00081 
00082         Logger::getInstance()->log(Logger::NOTICE,
00083                                  "GLX Context for window `" + mGLXWindow->getCaption() + "\' has been destroyed");
00084   }
00085 
00086  /*----------------------------------------------------------------------*/
00087 
00088   //
00089   // Methods
00090   //
00091 
00092   void GLXGLContext::initialize()
00093   {
00094         Assert(!mInitialized, INVALID_OPERATION_ERROR, "GLXGLContext::initialize()");
00095 
00096         // Lock and synchronize
00097         mDisplay.lock();
00098         mDisplay->synchronize();
00099 
00100         // Create GLX context
00101         mGLXContext = glXCreateContext(mDisplay->getDisplay(), mVisualInfo, NULL, True);
00102         Assert(mGLXContext, INVALID_OPERATION_ERROR, "GLXGLContext::initialize()");
00103 
00104         mDisplay.unlock();
00105 
00106         mInitialized = true;
00107         Logger::getInstance()->log(Logger::DEBUG,
00108                                  "GLX Context for window `" + mGLXWindow->getCaption() + "\' has been initialized: " +
00109                                  StringConverter::toString((void *) mGLXContext));
00110   }
00111 
00112  /*----------------------------------------------------------------------*/
00113 
00114   void GLXGLContext::finalize()
00115   {
00116         Assert(mInitialized, INVALID_OPERATION_ERROR, "GLXGLContext::finalize()");
00117 
00118         // Lock and synchronize
00119         mDisplay.lock();
00120         mDisplay->synchronize();
00121 
00122         // Destroy GLX context if it is not tainted
00123         Assert(mGLXContext, NULL_POINTER_ERROR, "GLXGLContext::finalize()");
00124         if(!mTainted)
00125          glXDestroyContext(mDisplay->getDisplay(), mGLXContext);
00126 
00127         mDisplay.unlock();
00128 
00129         mInitialized = false;
00130         Logger::getInstance()->log(Logger::DEBUG,
00131                                  "GLX Context for window `" + mGLXWindow->getCaption() + "\' has been finalized");
00132   }
00133 
00134  /*----------------------------------------------------------------------*/
00135 
00136   void GLXGLContext::setCurrent()
00137   {
00138         Assert(mInitialized, INVALID_OPERATION_ERROR, "GLXGLContext::setCurrent()");
00139 
00140         mDisplay.lock();
00141         if(glXMakeCurrent(mDisplay->getDisplay(), mGLXWindow->getWindow(), mGLXContext) != True)
00142         {
00143          Except(INTERNAL_ERROR, "GLXGLContext::setCurrent()", "calling glXMakeCurrent");
00144         }
00145         mDisplay.unlock();
00146   }
00147 
00148  /*----------------------------------------------------------------------*/
00149 
00150   void GLXGLContext::releaseCurrent()
00151   {
00152         Assert(mInitialized, INVALID_OPERATION_ERROR, "GLXGLContext::releaseCurrent()");
00153 
00154         mDisplay.lock();
00155         if(glXMakeCurrent(mDisplay->getDisplay(), None, 0) != True)
00156         {
00157          Except(INTERNAL_ERROR, "GLXGLContext::releaseCurrent()", "calling glXMakeCurrent");
00158         }
00159         mDisplay.unlock();
00160   }
00161 
00162  /*----------------------------------------------------------------------*/
00163 
00164 }