00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #if !defined SOGLCONTEXT_H
00024 #define SOGLCONTEXT_H
00025
00026 #include <Inventor/devices/SoDeviceContext.h>
00027 #include <Inventor/devices/SoGLFormat.h>
00028 #include <Inventor/STL/vector>
00029 #include <Inventor/STL/list>
00030 #include <Inventor/STL/map>
00031 #include <Inventor/helpers/SbGlContextHelper.h>
00032 #include <Inventor/threads/SbThreadSpinlock.h>
00033 #include <Inventor/errors/SoError.h>
00034 #include <Inventor/image/SbRasterImage.h>
00035 #include <Inventor/threads/SbThreadLocalStorage.h>
00036
00037
00038 #include <Inventor/SbPList.h>
00039 #include <Inventor/STL/stack>
00040
00041 #include <Inventor/helpers/SbGPUCapabilities.h>
00042
00043 #if defined(_WIN32)
00044 #include <windows.h>
00045 #endif
00046
00047
00048 #ifdef _MSC_VER
00049 #pragma warning( push )
00050 #pragma warning(disable:4251)
00051 #endif
00052
00053 class SbThreadMutex;
00054 namespace inventor
00055 {
00056 namespace helper
00057 {
00058 class FunctionRedirector;
00059 }}
00060
00196 class SoGLContext : public SoDeviceContext
00197 {
00198 public:
00199
00204 enum SharedGroupPolicy
00205 {
00207 DISABLED,
00208
00216 CONSERVATIVE,
00217
00224 AGGRESSIVE
00225 };
00226
00236 SoGLContext( bool shared );
00237
00249 SoGLContext( SbGlContextHelper::Display dpy,
00250 SbGlContextHelper::VisualInfo vis,
00251 SbGlContextHelper::Drawable drawable,
00252 SbGlContextHelper::GLContext ctx );
00253
00263 SoGLContext( const SoGLFormat& format, SbGlContextHelper::GLContext ctx, SbGlContextHelper::Drawable drawable = 0 );
00264
00272 SoGLContext( const SoGLFormat& format, SbGlContextHelper::Drawable drawable = 0 );
00273
00294 SoGLContext( SbGlContextHelper::Display dpy,
00295 SbGlContextHelper::VisualInfo vis,
00296 SbGlContextHelper::Drawable drawable,
00297 bool shared = true );
00298
00305 SoGLContext( SoGLContext* context, bool shared );
00306
00315 SoGLContext( SoGLContext* context, const SoGLFormat& format, SbGlContextHelper::Drawable drawable = 0 );
00316
00321 virtual void bind();
00322
00323
00328 virtual bool tryBind();
00329
00333 virtual void unbind();
00334
00340 bool isSharedWith( const SoGLContext* context ) const;
00341
00347 bool isValid() const;
00348
00354 void invalidate();
00355
00362 int getId() const;
00363
00373 virtual bool isCompatible( SoDeviceContext* context ) const;
00374
00379 virtual bool isCurrent() const;
00380
00400 static SoGLContext* getCurrent( bool checkGLState = false );
00401
00407 const SbGlContextHelper::GLContext& getGLContext() const;
00408
00414 static SoGLContext* findGLContext( SbGlContextHelper::GLContext );
00415
00421 #if defined(_WIN32)
00422 typedef int SharedGroupDescription;
00423 #else
00424 typedef SbGlContextHelper::Display SharedGroupDescription;
00425 #endif
00426 static SoGLContext* findSharedContext( SharedGroupDescription );
00427
00433 const SbGlContextHelper::Display& getDisplay() const;
00434
00440 const SbGlContextHelper::VisualInfo& getVisualInfo() const;
00441
00448 const SoGLFormat& getFormat() const;
00449
00456 static SoGLContext* getContextFromId( int id );
00457
00464 static SoGLContext* getContextFromSharedId( int sharedIdGroup );
00465
00470 bool isValidForCurrent() const;
00471
00475 void assertContext() const;
00476
00477 #if 1 SoDEPRECATED
00486 bool swapBuffers( int plane );
00487 #endif
00492 bool swapBuffers();
00493
00502 SbStringList getSupportedExtensions();
00503
00505 static SharedGroupPolicy getSharedGroupPolicy();
00506
00507 #if 1 SoDEPRECATED
00510 void applySharedGroupPolicy();
00511 #endif
00519 void setNoGLContextDelete() { m_noOGLContextDelete = true; }
00520
00525 const SbGPUCapabilities& getContextGraphicsCapabilities();
00526
00531 static const SbGPUCapabilities& getGraphicsCapabilities();
00532
00533 private:
00534
00535 SB_THREAD_TLS_HEADER();
00536
00537 static void initClass();
00538 static void exitClass();
00539
00541 enum GPUVendorType
00542 {
00543 GPU_TYPE_NOT_INITIALIZED = -1,
00544 GPU_NVIDIA = 0,
00545 GPU_ATI = 1,
00546 GPU_INTEL = 2,
00547 GPU_FIREPRO_MAC = 3
00548 };
00549
00550
00551 static GPUVendorType getGPUVendorType();
00552
00564 static SoGLContext* getActualCurrentContext( SoGLContext* supposedContext );
00565
00567 static void lockListsMutex();
00568
00570 static void unlockListsMutex();
00571
00575 void setUserData( void* data )
00576 { m_userData = data; }
00577
00579 void* getUserData() const
00580 { return m_userData; }
00581
00584 bool setSharedWith( SoGLContext* sharedContext );
00585
00586
00587 void forceDisplay( const SbGlContextHelper::Display& display );
00588
00589
00590 void forceDrawable( const SbGlContextHelper::Drawable& drawable );
00591
00592
00593
00594 void forceContext( const SbGlContextHelper::GLContext& context )
00595 { m_context = context; }
00596
00597
00598 inline void forceNoGLContextDelete(bool flag) { m_noOGLContextDelete = flag; }
00599
00600
00601 const SbGlContextHelper::Drawable& getDrawable() const
00602 { return m_drawable;}
00603
00604
00605 static void* glewGetCurrentContextIV(const bool printError=false)
00606 {
00607 SoGLContext* ctx = SoGLContext::getCurrent(true);
00608
00609 if ( ctx )
00610 return ctx->glewGetContextIV();
00611 else
00612 {
00613 if (printError)
00614 SoError::post("SoGLContext : Trying to access a NULL context");
00615 return glewNullContext();
00616 }
00617 };
00618
00619 # if defined(_WIN32)
00620 static void* wglewGetCurrentContextIV(const bool printError=false)
00621 {
00622 SoGLContext* ctx = SoGLContext::getCurrent(true);
00623
00624 if ( ctx )
00625 return ctx->wglewGetContextIV();
00626 else
00627 {
00628 if (printError)
00629 SoError::post("SoGLContext : Trying to access a NULL context");
00630 return NULL;
00631 }
00632 };
00633 # elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
00634 static void* glxewGetCurrentContextIV(const bool printError=false)
00635 {
00636 SoGLContext* ctx = SoGLContext::getCurrent(true);
00637
00638 if ( ctx )
00639 return ctx->glxewGetContextIV();
00640 else
00641 {
00642 if (printError)
00643 SoError::post("SoGLContext : Trying to access a NULL context");
00644 return NULL;
00645 }
00646 };
00647 # endif // _WIN32
00648
00649
00650 struct GLEWContextStruct* glewGetContextIV() const
00651 { return m_glewContext; }
00652
00653 # if defined(_WIN32)
00654 void* wglewGetContextIV() const
00655 { return m_wglewContext; }
00656
00657 # elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
00658 void* glxewGetContextIV() const
00659 { return m_glxewContext; }
00660
00661 # endif // _WIN32
00662
00663 bool isGLEWInitialized() { return m_glewInit; }
00664 bool initGLEW();
00665 void cleanGLEWContext();
00666
00667 virtual SbString getInfos();
00668
00669
00670 SbString getDeviceName() const;
00671
00675 void printInformation();
00676
00680 static void printCurrentContextInformation();
00681
00685 static void printContextsInformation();
00686
00692 static void invalidateDrawable(SbGlContextHelper::Drawable drawable);
00693
00699 static void invalidateDisplay(SbGlContextHelper::Display display);
00700
00719 bool setVSyncEnabled(bool on);
00720
00726 bool isVSyncEnabled();
00727
00729 bool isBinded();
00730
00731 private:
00735 virtual ~SoGLContext();
00736
00737 SoINTERNAL private:
00738
00739
00740
00741
00742 void removeSharedObjects();
00743
00744 private:
00745
00746
00747 static struct GLEWContextStruct* glewNullContext();
00748
00749 virtual bool setSharedWith( SoDeviceContext* sourceCtx );
00750
00756 void setSwapInterval(int interval);
00757
00758
00759 struct MTstruct
00760 {
00761 typedef std::vector<SoGLContext*> ContextVec;
00763 ContextVec* m_bindedContexts;
00764 };
00765
00766 bool initContext( SoGLContext* sourceContext, bool shared );
00767 void initGLEWContext();
00768 void preInitContext();
00769 bool postInitContext();
00770
00771 void checkContextParameters( const char* function );
00772
00773 typedef std::list< SoGLContext* > SoGLContextList;
00774
00776 static SoGLContextList s_contexts;
00777
00779 static int s_firstAvailableId;
00780
00782 static SbThreadSpinlock s_contextsMutex;
00783
00785 int m_id;
00786
00787 SbGlContextHelper::GLContext m_context;
00788 SbGlContextHelper::Drawable m_drawable;
00789
00790 SoGLFormat m_format;
00791
00792 void* m_userData;
00793
00794 static GPUVendorType m_GpuVendorType;
00795
00796
00797 struct GLEWContextStruct* m_glewContext;
00798
00799
00800 static struct GLEWContextStruct* s_nullGlewContext;
00801
00802 # if defined(_WIN32)
00803 struct WGLEWContextStruct* m_wglewContext;
00804 # elif !defined(__APPLE__) || defined(GLEW_APPLE_GLX)
00805 void* m_glxewContext;
00806 # endif // _WIN32
00807
00808 bool m_glewInit;
00809 bool m_noOGLContextDelete;
00810
00811 bool m_drawableIsValid;
00812 bool m_displayIsValid;
00813
00814 static int s_checkContext;
00815
00816 static int s_oivCompatibilityMode;
00817
00819 static SharedGroupPolicy s_sharedGroupPolicy;
00820
00821
00822 bool m_sharedObjectsRemoved;
00823
00824 static SoGLContext* findSoGLContext(SbGlContextHelper::GLContext ctx);
00825 void pushInBindedList();
00826 static void updateBindedList();
00827 static void updateBindedList(SbGlContextHelper::Display display, SbGlContextHelper::Drawable drawable, SbGlContextHelper::GLContext context);
00828 static SoGLContext* popBindedList();
00829
00830 bool m_thirdPartyContext;
00831 bool m_isBeingDestroyed;
00832 SbGPUCapabilities* m_graphicCapabilities;
00833 static inventor::helper::FunctionRedirector s_deleteContextRedirector;
00834
00835 static bool callOriginalDeleteContext(SbGlContextHelper::Display display, SbGlContextHelper::GLContext context, int& returnValue);
00836 static bool callOriginalMakeCurrent(SbGlContextHelper::Display display, SbGlContextHelper::Drawable drawable, SbGlContextHelper::GLContext context, int& returnValue);
00837 static int commonDeleteContextHook(SbGlContextHelper::Display dpy, SbGlContextHelper::GLContext ctx);
00838 static int commonMakeCurrent(SbGlContextHelper::Display display, SbGlContextHelper::Drawable drawable, SbGlContextHelper::GLContext context);
00839
00840 #if defined(WIN32)
00841 static int _stdcall makeCurrentHook(SbGlContextHelper::Display dpy, SbGlContextHelper::GLContext context);
00842 static int _stdcall deleteContextHook(SbGlContextHelper::GLContext context);
00843 #elif defined(__APPLE__)
00844 static int deleteContextHook(SbGlContextHelper::GLContext context);
00845 static int makeCurrentHook(SbGlContextHelper::GLContext context);
00846 #else
00847 static inventor::helper::FunctionRedirector s_makeContextCurrentRedirector;
00848 static int deleteContextHook(SbGlContextHelper::Display dpy, SbGlContextHelper::GLContext ctx);
00849 static int makeCurrentHook(SbGlContextHelper::Display dpy, SbGlContextHelper::Drawable drawable, SbGlContextHelper::GLContext ctx);
00850 static int makeContextCurrentHook(SbGlContextHelper::Display display, SbGlContextHelper::Drawable drawable, SbGlContextHelper::Drawable read, SbGlContextHelper::GLContext context);
00851 #endif
00852
00853 static MTstruct::ContextVec& getBindedContextsList();
00854 };
00855
00856 #ifdef _MSC_VER
00857 #pragma warning( pop )
00858 #endif
00859
00860
00861 #endif //SOGLCONTEXT_H
00862
00863