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/devices/SoGLGlew.h>
00029 #include <Inventor/STL/vector>
00030 #include <Inventor/STL/list>
00031 #include <Inventor/STL/map>
00032 #include <Inventor/helpers/SbGlContextHelper.h>
00033 #include <Inventor/threads/SbThreadLocalStorage.h>
00034 #include <Inventor/threads/SbThreadSpinlock.h>
00035 #include <Inventor/errors/SoError.h>
00036 #include <Inventor/image/SbRasterImage.h>
00037 #include <Inventor/threads/SbThreadLocalStorage.h>
00038
00039
00040 #include <Inventor/SbPList.h>
00041 #include <Inventor/STL/stack>
00042
00043 #include <Inventor/helpers/SbGPUCapabilities.h>
00044
00045 #if defined(_WIN32)
00046 #include <windows.h>
00047 #endif
00048
00049
00050 #ifdef _MSC_VER
00051 #pragma warning( push )
00052 #pragma warning(disable:4251)
00053 #endif
00054
00055 class SbThreadMutex;
00056 struct GLEWContextStruct;
00057
00059 namespace inventor
00060 {
00061 namespace helper
00062 {
00063 class FunctionRedirector;
00064 }}
00189 class SoGLContext : public SoDeviceContext
00190 {
00191 public:
00192
00197 enum SharedGroupPolicy
00198 {
00200 DISABLED,
00201
00209 CONSERVATIVE,
00210
00217 AGGRESSIVE
00218 };
00219
00229 SoGLContext( bool shared );
00230
00242 SoGLContext( SbGlContextHelper::Display dpy,
00243 SbGlContextHelper::VisualInfo vis,
00244 SbGlContextHelper::Drawable drawable,
00245 SbGlContextHelper::GLContext ctx );
00246
00256 SoGLContext( const SoGLFormat& format, SbGlContextHelper::GLContext ctx, SbGlContextHelper::Drawable drawable = 0 );
00257
00265 SoGLContext( const SoGLFormat& format, SbGlContextHelper::Drawable drawable = 0 );
00266
00287 SoGLContext( SbGlContextHelper::Display dpy,
00288 SbGlContextHelper::VisualInfo vis,
00289 SbGlContextHelper::Drawable drawable,
00290 bool shared = true );
00291
00298 SoGLContext( SoGLContext* context, bool shared );
00299
00308 SoGLContext( SoGLContext* context, const SoGLFormat& format, SbGlContextHelper::Drawable drawable = 0 );
00309
00314 virtual void bind();
00315
00316
00321 virtual bool tryBind();
00322
00326 virtual void unbind();
00327
00333 bool isSharedWith( const SoDeviceContext* context ) const;
00334
00336 virtual bool isValid() const;
00337
00343 void invalidate();
00344
00351 int getId() const;
00352
00362 virtual bool isCompatible( SoDeviceContext* context ) const;
00363
00368 virtual bool isCurrent() const;
00369
00385 static SoGLContext* getCurrent( bool checkGLState = false );
00386
00392 const SbGlContextHelper::GLContext& getGLContext() const;
00393
00399 static SoGLContext* findGLContext( SbGlContextHelper::GLContext );
00400
00406 #if defined(_WIN32)
00407 typedef int SharedGroupDescription;
00408 #else
00409 typedef SbGlContextHelper::Display SharedGroupDescription;
00410 #endif
00411 static SoGLContext* findSharedContext( SharedGroupDescription );
00412
00418 const SbGlContextHelper::Display& getDisplay() const;
00419
00425 const SbGlContextHelper::VisualInfo& getVisualInfo() const;
00426
00433 const SoGLFormat& getFormat() const;
00434
00441 static SoGLContext* getContextFromId( int id );
00442
00449 static SoGLContext* getContextFromSharedId( int sharedIdGroup );
00450
00455 bool isValidForCurrent() const;
00456
00460 void assertContext() const;
00461
00465 bool swapBuffers();
00466
00475 SbStringList getSupportedExtensions();
00476
00478 static SharedGroupPolicy getSharedGroupPolicy();
00479
00486 void setNoGLContextDelete() { m_noOGLContextDelete = true; }
00487
00492 const SbGPUCapabilities& getContextGraphicsCapabilities();
00493
00498 static const SbGPUCapabilities& getGraphicsCapabilities();
00499
00500 private:
00501
00502 SB_THREAD_TLS_HEADER();
00503
00504 static void initClass();
00505 static void exitClass();
00506
00508 enum GPUVendorType
00509 {
00510 GPU_TYPE_NOT_INITIALIZED = -1,
00511 GPU_NVIDIA = 0,
00512 GPU_ATI = 1,
00513 GPU_INTEL = 2,
00514 GPU_FIREPRO_MAC = 3
00515 };
00516
00517
00518 static GPUVendorType getGPUVendorType();
00519
00531 static SoGLContext* getActualCurrentContext( SoGLContext* supposedContext );
00532
00536 void setUserData( void* data )
00537 { m_userData = data; }
00538
00540 void* getUserData() const
00541 { return m_userData; }
00542
00545 bool setSharedWith( SoGLContext* sharedContext );
00546
00547
00548 void forceDisplay( const SbGlContextHelper::Display& display );
00549
00550
00551 void forceDrawable( const SbGlContextHelper::Drawable& drawable );
00552
00553
00554
00555 void forceContext( const SbGlContextHelper::GLContext& context )
00556 { m_context = context; }
00557
00558
00559 inline void forceNoGLContextDelete(bool flag) { m_noOGLContextDelete = flag; }
00560
00561
00562 const SbGlContextHelper::Drawable& getDrawable() const
00563 { return m_drawable;}
00564
00565 virtual SbString getInfos();
00566
00567
00568 SbString getDeviceName() const;
00569
00573 void printInformation();
00574
00578 static void printCurrentContextInformation();
00579
00583 static void printContextsInformation();
00584
00590 static void invalidateDrawable(SbGlContextHelper::Drawable drawable);
00591
00597 static void invalidateDisplay(SbGlContextHelper::Display display);
00598
00617 bool setVSyncEnabled(bool SO_UNUSED_PARAM(on));
00618
00624 bool isVSyncEnabled();
00625
00627 bool isBinded();
00628
00629 static GLEWContextStruct* glewGetCurrentContext();
00630 static SoGLGlew::GlewContextNative* glewGetCurrentContextNative();
00631
00632 private:
00636 virtual ~SoGLContext();
00637
00643 void removeSharedObjects();
00644
00645 private:
00646
00648 void checkRefCount() const;
00649
00650 virtual bool setSharedWith( SoDeviceContext* sourceCtx );
00651
00657 void setSwapInterval(int interval);
00658
00659
00660 struct MTstruct
00661 {
00662 typedef std::vector<SoGLContext*> ContextVec;
00664 ContextVec* m_bindedContexts;
00665 };
00666
00667 bool initContext( SoGLContext* sourceContext, bool shared );
00668 void preInitContext();
00669 bool postInitContext();
00670
00671 void checkContextParameters( const char* function );
00672
00673 typedef std::list< SoGLContext* > SoGLContextList;
00674
00676 static SoGLContextList s_contexts;
00677
00679 static int s_firstAvailableId;
00680
00682 static SbThreadSpinlock s_contextsMutex;
00683
00685 int m_id;
00686
00687 SbGlContextHelper::GLContext m_context;
00688 SbGlContextHelper::Drawable m_drawable;
00689
00694 SbGlContextHelper::GLContext m_savedContextForDelete;
00695
00696 SoGLFormat m_format;
00697
00698 void* m_userData;
00699
00700 static GPUVendorType m_GpuVendorType;
00701
00702 bool m_noOGLContextDelete;
00703
00704 bool m_drawableIsValid;
00705 bool m_displayIsValid;
00706
00707 static int s_checkContext;
00708
00709 static int s_oivCompatibilityMode;
00710
00712 static SharedGroupPolicy s_sharedGroupPolicy;
00713
00714
00715 bool m_sharedObjectsRemoved;
00716
00718 bool m_displayWarning;
00719
00720 static SoGLContext* findSoGLContext(SbGlContextHelper::GLContext ctx);
00721 void pushInBindedList();
00722 static void updateBindedList();
00723 static void updateBindedList(SbGlContextHelper::Display display, SbGlContextHelper::Drawable drawable, SbGlContextHelper::GLContext context);
00724 static SoGLContext* popBindedList();
00725
00726 bool m_thirdPartyContext;
00727 bool m_isBeingDestroyed;
00728 SbGPUCapabilities* m_graphicCapabilities;
00729 static inventor::helper::FunctionRedirector s_deleteContextRedirector;
00730
00731 static bool callOriginalDeleteContext(SbGlContextHelper::Display display, SbGlContextHelper::GLContext context, int& returnValue);
00732 static int commonDeleteContextHook(SbGlContextHelper::Display dpy, SbGlContextHelper::GLContext ctx);
00733
00734 #if defined(WIN32)
00735 static int _stdcall makeCurrentHook(SbGlContextHelper::Display dpy, SbGlContextHelper::GLContext context);
00736 static int _stdcall deleteContextHook(SbGlContextHelper::GLContext context);
00737 #elif defined(__APPLE__)
00738 static int deleteContextHook(SbGlContextHelper::GLContext context);
00739 #else
00740 static inventor::helper::FunctionRedirector s_makeContextCurrentRedirector;
00741 static int deleteContextHook(SbGlContextHelper::Display dpy, SbGlContextHelper::GLContext ctx);
00742 #endif
00743
00744 static MTstruct::ContextVec& getBindedContextsList();
00745 };
00746
00747 #ifdef _MSC_VER
00748 #pragma warning( pop )
00749 #endif
00750
00751
00752 #endif //SOGLCONTEXT_H
00753
00754