1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
45 #include "OSGConfig.h"
49 #include <boost/function.hpp>
51 #include "OSGDeprecatedCPP.h"
53 #include "OSGVector.h"
55 #include "OSGWindowBase.h"
56 #include "OSGWindowDrawTask.h"
57 #include "OSGWindowDrawThread.h"
58 #include "OSGDrawEnv.h"
62 class RenderActionBase
;
63 class TraversalValidator
;
65 class PassiveViewport
;
67 /*! \brief Window base class. See \ref PageSystemWindowWindow
70 \ingroup GrpSystemWindowBase
71 \ingroup GrpLibOSGSystem
75 class OSG_SYSTEM_DLLMAPPING Window
: public WindowBase
77 /*========================== PUBLIC =================================*/
82 * Enumeration values for the status of the GL objects. This is primarily
83 * used to signal the object's callback functions what to do. See \ref
84 * PageSystemOGLObjects for a description.
88 notused
= 0x0001, /**< Object is not used at all right now. */
89 initialize
= 0x0002, /**< Object is being initialized for the first time. */
90 reinitialize
= 0x0003, /**< Object is being re-initialized (ie. significant change) */
91 initialized
= 0x0004, /**< Object is initialized and valid. */
92 needrefresh
= 0x0005, /**< Object is initialized but needs a refresh. */
93 destroy
= 0x0006, /**< Object is to be destroyed (removed from context) */
94 finaldestroy
= 0x0007 /**< Object has been removed from contexts, now release resources. */
99 invalidExtensionID
= 0x7fffffff,
100 invalidFunctionID
= 0x7fffffff
105 SequentialPartitionDraw
= 0x000001,
106 ParallelPartitionDraw
= 0x000002,
107 PartitionDrawMask
= 0x0000FF,
109 StdDrawer
= 0x000100,
110 ParallelDrawer
= 0x000200,
111 DrawerMask
= 0x00FF00,
113 ActiveContext
= 0x010000,
114 ExternalContext
= 0x020000,
115 PassiveContext
= 0x040000,
116 ContextMask
= 0xFF0000
119 static const Real32 unknownConstant
;
122 /*---------------------------------------------------------------------*/
123 /*! \name Typedefs */
126 typedef UIntPointer GLObjectId
;
128 typedef boost::function
<UInt32 (DrawEnv
*,
131 UInt64
)> GLObjectFunctor
;
132 typedef boost::function
<void (DrawEnv
*,
134 GLObjectStatusE
)> GLObjectDestroyFunctor
;
136 typedef void (*GLExtensionFunction
)(void);
138 typedef WindowDrawTask::GLInitFunctor GLInitFunctor
;
140 /*---------------------------------------------------------------------*/
144 virtual void changed(ConstFieldMaskArg whichField
,
149 /*---------------------------------------------------------------------*/
150 /*! \name Viewport handling */
154 /*---------------------------------------------------------------------*/
155 /*! \name Extension registration
159 static UInt32
registerExtension(const Char8
*s
);
160 static void ignoreExtensions (const Char8
*s
);
162 static UInt32
registerFunction (const Char8
*s
,
164 UInt32 version
= 0xffff);
166 static void registerConstant ( GLenum val
);
169 /*---------------------------------------------------------------------*/
170 /*! \name Extension handling */
173 static void setGLLibraryName (const Char8
*s
);
175 UInt32
getGLVersion ( void );
177 static Int32
getExtensionIdX (const Char8
*s
);
178 bool hasExtension ( UInt32 extId
);
179 bool hasExtension (const Char8
*s
);
180 static bool hasCommonExtension( UInt32 extId
);
181 GLExtensionFunction
getFunction ( UInt32 funcId
);
182 GLExtensionFunction
getFunctionNoCheck( UInt32 funcId
);
183 void dumpExtensions ( void );
184 GLExtensionFunction
getFunctionByName (const Char8
*s
);
185 Real32
getConstantValue ( GLenum val
);
186 const Vec2f
&getConstantValuev ( GLenum val
);
189 bool hasExtOrVersion (UInt32 extId
,
190 UInt32 uiGLVersion
= 0xFFFF,
191 UInt32 uiGLESVersion
= 0xFFFF);
194 /*---------------------------------------------------------------------*/
195 /*! \name Extension string handling */
198 static const std::vector
<std::string
> &getRegisteredExtensions(void);
199 const std::vector
<std::string
> &getRegisteredFunctions (void);
200 const std::vector
<std::string
> &getExtensions (void);
201 const std::vector
<std::string
> &getIgnoredExtensions (void);
204 /*---------------------------------------------------------------------*/
205 /*! \name GL object registration */
208 static UInt32
registerGLObject(GLObjectFunctor functor
,
209 GLObjectDestroyFunctor destroyFunctor
,
213 /*---------------------------------------------------------------------*/
214 /*! \name GL object handling */
217 UInt32
validateGLObject (UInt32 osgId
,
219 UInt64 uiOptions
= 0);
220 void validateAllGLObjects(void);
222 void setGLObjectId (UInt32 osgId
,
224 GLObjectId
getGLObjectId (UInt32 osgId
);
226 void setGLObjectInfo (UInt32 osgId
,
228 GLObjectId
getGLObjectInfo (UInt32 osgId
);
231 /*---------------------------------------------------------------------*/
232 /*! \name static GL object handling */
235 static UInt32
getGLObjectsSize (void );
237 static void refreshGLObject (UInt32 osgId
);
238 static void refreshAllGLObjects (void );
240 static void reinitializeGLObject (UInt32 osgId
);
241 static void reinitializeAllGLObjects(void );
243 static void destroyGLObject (UInt32 osgId
,
247 /*---------------------------------------------------------------------*/
248 /*! \name Size handling */
251 virtual void resize (int width
,
254 void setSize (UInt16 width
,
258 /*---------------------------------------------------------------------*/
262 virtual void render (RenderActionBase
*action
);
263 virtual void renderNoFinish(RenderActionBase
*action
);
264 virtual void frameFinish (bool bActivate
= true);
265 virtual void runFrameExit (void );
268 /*---------------------------------------------------------------------*/
269 /*! \name Window system implementation functions */
272 virtual void init(GLInitFunctor oFunc
= GLInitFunctor());
275 /*---------------------------------------------------------------------*/
276 /*! \name Sequential drawing */
279 virtual void activate (void );
280 virtual void deactivate (void );
281 virtual bool swap (void );
283 virtual void frameExit (void );
284 virtual void frameInit (void );
285 virtual void renderAllViewports(RenderActionBase
*action
);
287 virtual void terminate (void ) = 0;
290 /*---------------------------------------------------------------------*/
294 void setPartitionDrawMode(UInt32 uiMode
);
295 void setDrawerType (UInt32 uiDrawerType
);
297 UInt32
getPartitionDrawMode(void ) const;
298 UInt32
getDrawerType (void ) const;
301 /*---------------------------------------------------------------------*/
305 ShaderCache
*getShaderCache (void );
306 TraversalValidator
*getTravValidator(void );
308 static void requestStageRun (Int32 iStageId
);
311 /*---------------------------------------------------------------------*/
315 virtual void resolveLinks(void);
318 /*---------------------------------------------------------------------*/
322 void queueTaskFromDrawer(DrawTask
*pTask
);
323 void queueTask (DrawTask
*pTask
);
324 static void queueGlobalTask (DrawTask
*pTask
);
327 void clearDrawTasks (void );
330 /*---------------------------------------------------------------------*/
334 virtual void submitTask (HardwareContextTask
*pTask
);
335 virtual void submitTaskAndWait(BlockingTask
*pTask
);
338 /*---------------------------------------------------------------------*/
342 virtual void dump ( UInt32 uiIndent
= 0,
343 const BitVector bvFlags
= 0) const;
345 static void staticDump(void );
348 /*========================= PROTECTED ===============================*/
352 /*---------------------------------------------------------------------*/
353 /*! \name Constructors */
357 Window(const Window
&source
);
360 /*---------------------------------------------------------------------*/
361 /*! \name Destructors */
364 virtual ~Window(void);
367 /*---------------------------------------------------------------------*/
368 /*! \name GL setup handling */
371 virtual void setupGL(void);
374 /*---------------------------------------------------------------------*/
378 static void initMethod(InitPhase ePhase
);
379 static bool cleanup (void );
382 /*---------------------------------------------------------------------*/
383 /*! \name Window system implementation functions */
386 void validateAllGLObjects(DrawEnv
*pEnv
);
389 /*---------------------------------------------------------------------*/
390 /*! \name Window system implementation functions */
393 virtual void doTerminate (void );
395 virtual void doActivate (void ) = 0;
396 virtual void doDeactivate (void ) = 0;
397 virtual bool doSwap (void ) = 0;
399 virtual void doFrameInit (bool reinitExtFuctions
= false);
400 virtual void doFrameExit (void );
402 virtual void doRenderAllViewports(RenderActionBase
*action
);
404 virtual bool hasContext (void ) = 0;
407 /*---------------------------------------------------------------------*/
408 /*! \name GL object handling */
411 void setupTasks(void);
414 /*---------------------------------------------------------------------*/
415 /*! \name GL object handling */
418 static void doRefreshGLObject (UInt32 osgId
);
419 static void doReinitializeGLObject(UInt32 osgId
);
421 static void resetGLObjectStatus (UInt32 osgId
, UInt32 num
);
422 void doResetGLObjectStatus (UInt32 osgId
, UInt32 num
);
425 /*---------------------------------------------------------------------*/
426 /*! \name GL object handling helper class */
431 friend class GLObject
;
433 /*! Used to keep track of the OpenGL objects registered with the system.
434 See \ref PageSystemOGLObjects for a description.
442 GLObject(GLObjectFunctor funct
,
443 GLObjectDestroyFunctor destroy
);
445 void acquireLock(void);
446 void releaseLock(void);
448 GLObjectFunctor
& getFunctor(void );
449 void setFunctor(GLObjectFunctor funct
);
451 GLObjectDestroyFunctor
& getDestroyFunctor(void );
452 void setDestroyFunctor(GLObjectDestroyFunctor funct
);
454 UInt32
getRefCounter(void);
455 UInt32
incRefCounter(void);
456 UInt32
decRefCounter(void);
460 GLObjectFunctor _functor
;
461 GLObjectDestroyFunctor _destroy
;
462 volatile UInt32 _refCounter
;
466 /*---------------------------------------------------------------------*/
467 /*! \name MT Construction */
470 void onCreate (const Window
*source
= NULL
);
472 void onCreateAspect (const Window
*createAspect
,
473 const Window
*source
= NULL
);
475 void onDestroy ( UInt32 uiContainerId
);
477 void onDestroyAspect( UInt32 uiContainerId
,
480 static void staticAcquire (void );
481 static void staticRelease (void );
484 /*---------------------------------------------------------------------*/
485 /*! \name Map for GL cvonstant handling */
488 typedef OSG_HASH_MAP(GLenum
, Vec2f
) ConstHash
;
491 /*---------------------------------------------------------------------*/
492 /*! \name Map for GL cvonstant handling */
495 void pushToDrawTasks(DrawTask
* const value
);
498 /*---------------------------------------------------------------------*/
499 /*! \name GL object handling */
503 /*========================== PRIVATE ================================*/
507 typedef WindowBase Inherited
;
509 typedef std::vector
<WindowMTUncountedPtr
> WindowStore
;
511 friend class FieldContainer
;
512 friend class WindowBase
;
513 friend class DrawTask
;
514 friend class WindowDrawTask
;
515 friend class PassiveViewport
;
517 static WindowStore _allWindows
;
518 static Int32 _currentWindowId
;
520 /*---------------------------------------------------------------------*/
521 /*! \name Static GL Object / Extension variables */
524 static LockRefPtr _GLObjectLock
;
525 static LockRefPtr _staticWindowLock
;
527 static std::vector
<GLObject
*> _glObjects
;
528 static const Char8
*_glLibraryName
;
531 * List of registered extension names. The index of the entry is the
532 * id/handle of the extention.
534 static std::vector
<std::string
> _registeredExtensions
;
535 /** List of names of extensions to ignore. */
536 static std::vector
<std::string
> _ignoredExtensions
;
537 static std::vector
<bool > _commonExtensions
; /**< ??? */
538 static std::vector
<std::string
> _registeredFunctions
;
539 static std::vector
<Int32
> _registeredFunctionExts
;
540 static std::vector
<UInt32
> _registeredFunctionVersions
;
542 static std::vector
<GLenum
> _registeredConstants
;
544 typedef std::pair
<UInt32
,UInt32
> DestroyEntry
;
546 std::list
<DestroyEntry
> _glObjectDestroyList
;
549 /*---------------------------------------------------------------------*/
550 /*! \name GL Object / Extension variables */
553 std::vector
<UInt32
> _lastValidate
;
554 std::vector
<GLObjectId
> _ids
;
555 std::vector
<GLObjectId
> _idInfo
;
558 std::vector
<std::string
> _extensions
;
560 * List of whether extension can be used in current context. Indexed by
561 * extension id (registeredExtensions).
563 std::vector
<bool > _availExtensions
;
564 std::vector
<GLExtensionFunction
> _extFunctions
;
566 ConstHash _availConstants
;
569 TraversalValidator
*_pTravValidator
;
570 ShaderCache
*_pShaderCache
;
572 WindowDrawTaskRefPtr _pInitTask
;
573 WindowDrawTaskRefPtr _pWaitTask
;
574 WindowDrawTaskRefPtr _pSwapTask
;
575 WindowDrawTaskRefPtr _pFrameInitTask
;
576 WindowDrawTaskRefPtr _pFrameExitTask
;
577 WindowDrawTaskRefPtr _pActivateTask
;
578 WindowDrawTaskRefPtr _pDeactivateTask
;
579 DrawTaskRefPtr _pGLFinishTask
;
584 /*---------------------------------------------------------------------*/
586 // prohibit default functions (move to 'public' if you need one)
587 Window
&operator =(const Window
&source
);
590 //---------------------------------------------------------------------------
592 //---------------------------------------------------------------------------
594 typedef Window
*WindowP
;
598 #include "OSGWindowBase.inl"
599 #include "OSGWindow.inl"
601 #endif /* _OSGWINDOW_H_ */