GL Studio Safety Critical Embedded C++ Runtime Library
gls_state_manager.h
Go to the documentation of this file.
1 #ifndef _GLS_STATE_MANAGER_H
2 #define _GLS_STATE_MANAGER_H
3 
4 /*! \file gls_state_manager.h
5 \brief This header defines the GL State Manager class for managing the GL
6  state in the GL Studio DO-178B Runtime Library.
7 
8 \par Copyright Information
9 Copyright (C) 1999-2012 The DiSTI Corporation<br>
10 Orlando, FL USA<br>
11 All rights reserved.<br>
12 
13  This file is copyrighted software and contains proprietary trade secrets of
14 DiSTI, and embodies substantial creative efforts as well as confidential
15 information, ideas, and expressions.
16 
17  Permission to use, and copy this software and its documentation for any
18 purpose is hereby granted per the Distribution Agreement and/or the Licensing
19 Agreement signed with DiSTI. This permission is granted provided that:
20  1. The above copyright notice appears in all copies.
21  2. That both the copyright notice and this permission notice appear in
22  the supporting documentation.
23  3. That the names DiSTI and GL Studio not be used in advertising or
24  publicity pertaining to distribution of the software without specific,
25  written prior permission of DiSTI.
26 
27  Permission to modify the software is granted, but not the right to
28 distribute the source code whether modified, or non-modified. Modifying the
29 software might invalidate the DO-178B certification package.
30 
31  Permission to distribute binaries produced by compiling source code, or
32 modified source code is granted, provided you:
33  1. Provide your name and address as the primary contact for the support
34  of your modified version.
35  2. Retain our contact information in regard to use of the base software.
36 
37  DiSTI does not provide warranty for this software or guarantee that it
38 satisfies any specification or requirement unless otherwise stated in a
39 specific contractual arrangement between the customer and DiSTI.
40 
41 */
42 
43 #include "gls_include.h"
44 #include "gls_gl.h"
45 #include "gls_color.h"
46 #include "gls_class_invariant.h"
47 
48 /** This class manages OpenGL context state and allows for minimizing calls to OpenGL.
49  * \invariant
50  * GlsGLAlphaFuncIsValid( _alphaFunc ),
51  * ( 0.0f <= _alphaRef ) && ( _alphaRef <= 1.0f ),
52  * GlsGLBlendFuncSFactorIsValid( _alphaBlendSFactor ),
53  * GlsGLBlendFuncDFactorIsValid( _alphaBlendDFactor ),
54  * GlsGLDepthFuncIsValid( _depthFunc ),
55  * GlsGLShadeModelIsValid( _shadeModel ),
56  * _color.IsValid(),
57  * ( ( GlsStateManager::LINE_WIDTH_MIN <= _lineWidth ) && ( _lineWidth <= GlsStateManager::LINE_WIDTH_MAX ) ),
58  * GlsGLPolygonModeIsValid( _polygonMode ) ),
59  * ( ( 0 == _glBeginCount ) || ( 1 == _glBeginCount ) ),
60  * ( 1 == _numInstCreated ),
61  * OpenGL state is synchronized with the state manager
62  */
64 {
65 public:
67 
68  #if defined( GLS_DEBUG )
69  static const GLfloat LINE_WIDTH_MIN; /**< mimimum line width */
70  static const GLfloat LINE_WIDTH_MAX; /**< maximum line width */
71  #endif // GLS_DEBUG
72 
73  /** invalid GL texture index */
74  static const GLuint INVALID_GL_TEXTURE_INDEX = 0u;
75 
76  /** Get the singleton GlsStateManager instance
77  * \pre precondition for constructor must hold first time this method is called
78  * \post postcondition for constructor holds after the first time this method is called
79  * \return the singleton GlsStateManager instance
80  */
81  static GlsStateManager& GetStateManager( void );
82 
83  /** Reset the cached GL state variables and set them into the GL state
84  * \pre none
85  * \post cached GL state variables are set to default values and
86  * GL is synchronized with state manager
87  */
88  void ResetOpenGLState( void );
89 
90  /** equivalent to glBegin() call
91  * \param mode primitive mode to begin
92  * \pre must not be within a begin/end pair,
93  * mode is a valid GL primitive mode
94  * \post a new primitive is started
95  */
96  void Begin( const GLenum mode );
97 
98  /** equivalent to glEnd() call
99  * \pre must be called within a begin/end pair
100  * \post primitive is ended
101  */
102  void End( void );
103 
104  /** equivalent to glBindTexture( GL_TEXTURE_2D, .. ) call
105  * \param textureIndex GL texture index of texture to bind ( >0 )
106  * \pre textureIndex > 0, textureIndex is a valid GL texture index,
107  * not within a begin/end pair
108  * \post desired 2D texture is bound to GL
109  */
110  void BindTexture2D( const GLuint textureIndex );
111 
112  /** equivalent to glLineWidth() call
113  * \param lineWidth new line width ( GlsStateManager::LINE_WIDTH_MIN <= width <= GlsStateManager::LINE_WIDTH_MAX )
114  * \pre GlsStateManager::LINE_WIDTH_MIN <= width <= GlsStateManager::LINE_WIDTH_MAX,
115  * not within a begin/end pair
116  * \post GL has new line width
117  */
118  void LineWidth( const GLfloat lineWidth );
119 
120  /** equivalent to glAlphaFunc() call
121  * \param func GL alpha comparison mode
122  * \param ref GL alpha reference value
123  * \pre GlsGLAlphaFuncIsValid( func ),
124  0.0f <= ref <= 1.0f, not within a begin/end pair
125  * \post new GL alpha mode and reference are set
126  */
127  void AlphaFunc( const GLenum func, const GLclampf ref );
128 
129  /** equivalent to glColor() call
130  * \param color new color
131  * \pre color.IsValid()
132  * \post new GL color is set
133  */
134  void Color( const GlsColor &color );
135 
136  /** equivalent to glBlendFunc() call
137  * \param sfactor alpha source mode
138  * \param dfactor alpha destination mode
139  * \pre GlsGLBlendFuncSFactorIsValid( sfactor ),
140  * GlsGLBlendFuncDFactorIsValid( dfactor ),
141  * not within a begin/end pair
142  * \post new GL alpha blend mode is set
143  */
144  void BlendFunc( const GLenum sfactor, const GLenum dfactor );
145 
146  /** equivalent to glShadeModel() call
147  * \param mode new GL shade mode
148  * \pre GlsGLShadeModelIsValid( mode ), not within a begin/end pair
149  * \post new GL shade model is set
150  */
151  void ShadeModel( const GLenum mode );
152 
153  /** equivalent to glDepthFunc() call
154  * \param func new GL depth function
155  * \pre GlsGLDepthFuncIsValid( func ), not within a begin/end pair
156  * \post new GL depth function is set
157  */
158  void DepthFunc( const GLenum func );
159 
160  /** equivalent to glEnable(GL_ALPHA_TEST)/glDisable(GL_ALPHA_TEST) call
161  * \param enable GLS_TRUE to enable else GLS_FALSE
162  * \pre not within a begin/end pair
163  * \post GL_ALPHA_TEST is enabled if enable == GLS_TRUE else,
164  * GL_ALPHA_TEST is disabled
165  */
166  void EnableAlphaTest( const GlsBool enable );
167 
168  /** equivalent to glEnable(GL_BLEND)/glDisable(GL_BLEND) call
169  * \param enable GLS_TRUE to enable else GLS_FALSE
170  * \pre not within a begin/end pair
171  * \post GL_BLEND is enabled if enable == GLS_TRUE else,
172  * GL_BLEND is disabled
173  */
174  void EnableBlend( const GlsBool enable );
175 
176  /** equivalent to glEnable(GL_CULL_FACE)/glDisable(GL_CULL_FACE) call
177  * \param enable GLS_TRUE to enable else GLS_FALSE
178  * \pre not within a begin/end pair
179  * \post GL_CULL_FACE is enabled if enable == GLS_TRUE else,
180  * GL_CULL_FACE is disabled
181  */
182  void EnableCullFace( const GlsBool enable );
183 
184  /** equivalent to glEnable(GL_DEPTH_TEST)/glDisable(GL_DEPTH_TEST) call
185  * \param enable GLS_TRUE to enable else GLS_FALSE
186  * \pre not within a begin/end pair
187  * \post GL_DEPTH_TEST is enabled if enable == GLS_TRUE else,
188  * GL_DEPTH_TEST is disabled
189  */
190  void EnableDepthTest( const GlsBool enable );
191 
192  /** equivalent to glEnable(GL_LINE_SMOOTH)/glDisable(GL_LINE_SMOOTH) call
193  * \param enable GLS_TRUE to enable else GLS_FALSE
194  * \pre not within a begin/end pair
195  * \post GL_LINE_SMOOTH is enabled if enable == GLS_TRUE else,
196  * GL_LINE_SMOOTH is disabled
197  */
198  void EnableLineSmooth( const GlsBool enable );
199 
200  /** equivalent to glEnable(GL_TEXTURE_2D)/glDisable(GL_TEXTURE_2D) call
201  * \param enable GLS_TRUE to enable else GLS_FALSE
202  * \pre not within a begin/end pair
203  * \post GL_TEXTURE_2D is enabled if enable == GLS_TRUE else,
204  * GL_TEXTURE_2D is disabled
205  */
206  void EnableTexture2D( const GlsBool enable );
207 
208  /** equivalent to glTexParameteri(GL_TEXTURE_2D,...) call
209  * \param pname GL texture parameter name
210  * \param param new value for indicated parameter
211  * \pre GlsGLTextureParameterIsValid( pname ),
212  * pname == GL_TEXTURE_MIN_FILTER -> GlsGLTextureMinFilterIsValid( param ),
213  * pname == GL_TEXTURE_MAG_FITLER -> GlsGLTextureMagFilterIsValid( param ),
214  * pname == GL_TEXTURE_WRAP_S -> GlsGLTextureWrapIsValid( param ),
215  * pname == GL_TEXTURE_WRAP_T -> GlsGLTextureWrapIsValid( param ),
216  * not within a begin/end pair
217  * \post given GL texture parameter is set to new value
218  */
219  void TexParameteri2D( const GLenum pname, const GLint param );
220 
221  /** equivalent to glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,...) call
222  * \param param new value for texture environment mode
223  * \pre GlsGLTetxureEnvModeIsValid( param ),
224  * not within a begin/end pair
225  * \post GL texture environment mode is set to new value
226  */
227  void TexEnvi( const GLint param );
228 
229  #if defined( GLS_DEBUG )
230  /** Determine how many state manager instances that have been created ( GLS_DEBUG only )
231  * \return numner of state manager instances that have been created
232  * \pre none
233  * \post none
234  */
235  static GlsUInt32 GetNumberOfInstancesCreated( void );
236  #endif // GLS_DEBUG
237 
238 protected:
239  /** Constructor - create an instance
240  * \pre not within a begin/end pair, glGetError() == GL_NO_ERROR,
241  * no other state manager has been created
242  * \post instance created, OpenGL state is synchronized with state manager
243  */
244  GlsStateManager( void );
245 
246  GLenum _alphaFunc; /**< current GL alpha function */
247  GLclampf _alphaRef; /**< current GL alpha reference value */
248  GlsBool _alphaTest; /**< current GL Alpha Testing state (Screen door transparency) */
249 
250  GlsBool _alphaBlend; /**< current GL Alpha Blending state */
251  GLenum _alphaBlendSFactor; /**< current GL Alpha Blending source factor */
252  GLenum _alphaBlendDFactor; /**< current GL Alpha Blending destination factor */
253 
254  GlsBool _depthTest; /**< current GL Depth Testing state */
255  GLenum _depthFunc; /**< current GL Depth Testing function */
256 
257  GlsBool _lineSmooth; /**< current GL Line anti-aliasing */
258 
259  GlsBool _cullFace; /**< current GL Backface culling state */
260 
261  GLenum _shadeModel; /**< current GL shading model */
262 
263  GlsColor _color; /**< current GL Drawing color */
264 
265  GlsBool _texture2D; /**< current GL 2D texture state */
266 
267  GLuint _textureIndex2D; /**< current index of GL 2D bound texture else
268  * GlsStateManager::INVALID_GL_TEXTURE_INDEX if no texture is bound */
269 
270  GLfloat _lineWidth; /**< current GL Line width */
271 
272  GLenum _polygonMode; /**< current GL Polygon Draw Mode */
273 
274  #if defined( GLS_DEBUG )
275  GlsInt32 _glBeginCount; /**< begin / end pair indicator ( GLS_DEBUG only ) */
276  static GlsUInt32 _numInstCreated; /**< number of state managers that have been instantiated
277  * ( GLS_DEBUG only ) */
278  #endif
279 
280  /** Destructor - shall never be called
281  * \pre none
282  * \post none
283  */
284  virtual ~GlsStateManager();
285 
286 private:
287  // Disable implicit generated Members
288  GlsStateManager& operator=( const GlsStateManager &rhs );
289  GlsStateManager( const GlsStateManager &src );
290 };
291 
292 #if defined( GLS_DEBUG )
293 #pragma BullseyeCoverage save off
294 /** Determine if the given GL alpha function enum is valid ( GLS_DEBUG only )
295  * \param alphaFunc GL alpha function enum in question
296  * \return GLS_TRUE if valid else GLS_FALSE
297  * \pre none
298  * \post none
299  */
300 inline GlsBool GlsGLAlphaFuncIsValid( const GLenum alphaFunc )
301 {
302  return( ( GL_NEVER == alphaFunc ) || ( GL_LESS == alphaFunc ) || ( GL_EQUAL == alphaFunc ) ||
303  ( GL_LEQUAL == alphaFunc ) || ( GL_GREATER == alphaFunc ) || ( GL_NOTEQUAL == alphaFunc ) ||
304  ( GL_GEQUAL == alphaFunc ) || ( GL_ALWAYS == alphaFunc ) );
305 }
306 #pragma BullseyeCoverage restore
307 #endif // GLS_DEBUG
308 
309 
310 #if defined( GLS_DEBUG )
311 #pragma BullseyeCoverage save off
312 /** Determine if the given GL blend source factor enum is valid ( GLS_DEBUG only )
313  * \param sfactor GL blend source factor enum in question
314  * \return GLS_TRUE if valid else GLS_FALSE
315  */
316 inline GlsBool GlsGLBlendFuncSFactorIsValid( const GLenum sfactor )
317 {
318  return( ( GL_ZERO == sfactor ) || ( GL_ONE == sfactor ) ||
319  ( GL_DST_COLOR == sfactor ) || ( GL_ONE_MINUS_DST_COLOR == sfactor ) ||
320  ( GL_SRC_ALPHA == sfactor ) || ( GL_ONE_MINUS_SRC_ALPHA == sfactor ) ||
321  ( GL_DST_ALPHA == sfactor ) || ( GL_ONE_MINUS_DST_ALPHA == sfactor ) ||
322  ( GL_SRC_ALPHA_SATURATE == sfactor ) );
323 }
324 #pragma BullseyeCoverage restore
325 #endif // GLS_DEBUG
326 
327 #if defined( GLS_DEBUG )
328 #pragma BullseyeCoverage save off
329 /** Determine if the given GL blend dest factor enum is valid ( GLS_DEBUG only )
330  * \param dfactor GL blend dest factor enum in question
331  * \return GLS_TRUE if valid else GLS_FALSE
332  */
333 inline GlsBool GlsGLBlendFuncDFactorIsValid( const GLenum dfactor )
334 {
335  return( ( GL_ZERO == dfactor ) || ( GL_ONE == dfactor ) ||
336  ( GL_SRC_COLOR == dfactor ) || ( GL_ONE_MINUS_SRC_COLOR == dfactor ) ||
337  ( GL_SRC_ALPHA == dfactor ) || ( GL_ONE_MINUS_SRC_ALPHA == dfactor ) ||
338  ( GL_DST_ALPHA == dfactor ) || ( GL_ONE_MINUS_DST_ALPHA == dfactor ) );
339 }
340 #pragma BullseyeCoverage restore
341 #endif // GLS_DEBUG
342 
343 #if defined( GLS_DEBUG )
344 #pragma BullseyeCoverage save off
345 /** Determine if the given GL depth function enum is valid ( GLS_DEBUG only )
346  * \param func GL depth function enum in question
347  * \return GLS_TRUE if valid else GLS_FALSE
348  */
349 inline GlsBool GlsGLDepthFuncIsValid( const GLenum func )
350 {
351  return( ( GL_NEVER == func ) || ( GL_LESS == func ) || ( GL_LEQUAL == func ) ||
352  ( GL_EQUAL == func ) || ( GL_GREATER == func ) || ( GL_NOTEQUAL == func ) ||
353  ( GL_GEQUAL == func ) || ( GL_ALWAYS == func ) );
354 }
355 #pragma BullseyeCoverage restore
356 #endif // GLS_DEBUG
357 
358 #if defined( GLS_DEBUG )
359 #pragma BullseyeCoverage save off
360 /** Determine if the given GL shade model enum is valid ( GLS_DEBUG only )
361  * \param mode GL shade model in question
362  * \return GLS_TRUE if valid else GLS_FALSE
363  */
364 inline GlsBool GlsGLShadeModelIsValid( const GLenum mode )
365 {
366  return( ( GL_FLAT == mode ) || ( GL_SMOOTH == mode ) );
367 }
368 #pragma BullseyeCoverage restore
369 #endif // GLS_DEBUG
370 
371 #if defined( GLS_DEBUG )
372 #pragma BullseyeCoverage save off
373 /** Determine if the given GL texture environment mode is valid ( GLS_DEBUG only )
374  * \param mode GL texture environment mode in question
375  * \return GLS_TRUE if valid else GLS_FALSE
376  */
377 inline GlsBool GlsGLTetxureEnvModeIsValid( const GLint mode )
378 {
379  return( ( GL_MODULATE == mode ) || ( GL_DECAL == mode ) ||
380  ( GL_BLEND == mode ) || ( GL_REPLACE == mode ) );
381 }
382 #pragma BullseyeCoverage restore
383 #endif // GLS_DEBUG
384 
385 #if defined( GLS_DEBUG )
386 #pragma BullseyeCoverage save off
387 /** Determine if the given GL texture parameter is valid ( GLS_DEBUG only )
388  * \param pname GL texture parameter in question
389  * \return GLS_TRUE if valid else GLS_FALSE
390  */
391 inline GlsBool GlsGLTextureParameterIsValid( const GLenum pname )
392 {
393  return( ( GL_TEXTURE_MIN_FILTER == pname ) || ( GL_TEXTURE_MAG_FILTER == pname ) ||
394  ( GL_TEXTURE_WRAP_S == pname ) || ( GL_TEXTURE_WRAP_T == pname ) );
395 }
396 #pragma BullseyeCoverage restore
397 #endif // GLS_DEBUG
398 
399 #if defined( GLS_DEBUG )
400 #pragma BullseyeCoverage save off
401 /** Determine if the given GL texture minification mode is valid ( GLS_DEBUG only )
402  * \param param GL texture minification mode in question
403  * \return GLS_TRUE if valid else GLS_FALSE
404  */
405 inline GlsBool GlsGLTextureMinFilterIsValid( const GLint param )
406 {
407  return( ( GL_NEAREST == param ) || ( GL_LINEAR == param ) ||
408  ( GL_NEAREST_MIPMAP_NEAREST == param ) || ( GL_LINEAR_MIPMAP_NEAREST == param ) ||
409  ( GL_NEAREST_MIPMAP_LINEAR == param ) || ( GL_LINEAR_MIPMAP_LINEAR == param ) );
410 }
411 #pragma BullseyeCoverage restore
412 #endif // GLS_DEBUG
413 
414 #if defined( GLS_DEBUG )
415 #pragma BullseyeCoverage save off
416 /** Determine if the given GL texture magnification mode is valid ( GLS_DEBUG only )
417  * \param param GL texture magnification mode in quesion
418  * \return GLS_TRUE if valid else GLS_FALSE
419  */
420 inline GlsBool GlsGLTextureMagFilterIsValid( const GLint param )
421 {
422  return( ( GL_NEAREST == param ) || ( GL_LINEAR == param ) );
423 }
424 #pragma BullseyeCoverage restore
425 #endif // GLS_DEBUG
426 
427 #if defined( GLS_DEBUG )
428 #pragma BullseyeCoverage save off
429 /** Determine if the given GL texture wrap mode is valid ( GLS_DEBUG only )
430  * \param param GL texture wrap mode in question
431  * \return GLS_TRUE if valid else GLS_FALSE
432  */
433 inline GlsBool GlsGLTextureWrapIsValid( const GLint param )
434 {
435  return( ( GL_CLAMP == param ) || ( GL_REPEAT == param ) );
436 }
437 #pragma BullseyeCoverage restore
438 #endif // GLS_DEBUG
439 
440 #if defined( GLS_DEBUG )
441 #pragma BullseyeCoverage save off
442 /** Determine if the given GL polygon mode is valid ( GLS_DEBUG only )
443  * \param mode GL polygon mode in question
444  * \return GLS_TRUE if valid else GLS_FALSE
445  */
446 inline GlsBool GlsGLPolygonModeIsValid( const GLenum mode )
447 {
448  return( ( GL_POINT == mode ) || ( GL_LINE == mode ) || ( GL_FILL == mode ) );
449 }
450 #pragma BullseyeCoverage restore
451 #endif // GLS_DEBUG
452 
453 #endif // _GLS_STATE_MANAGER_H
int GlsInt32
Definition: gls_types.h:66
Definition: gls_color.h:47
bool GlsBool
Definition: gls_types.h:96
static GlsStateManager & GetStateManager(void)
void ResetOpenGLState(void)
#define GLS_CLASS_INVARIANT_DECLARATION(ClassName)
Definition: gls_class_invariant.h:80
GlsBool _lineSmooth
Definition: gls_state_manager.h:257
void ShadeModel(const GLenum mode)
Definition: gls_state_manager.h:63
void Color(const GlsColor &color)
GlsColor _color
Definition: gls_state_manager.h:263
GlsBool _depthTest
Definition: gls_state_manager.h:254
void BindTexture2D(const GLuint textureIndex)
unsigned int GlsUInt32
Definition: gls_types.h:73
This header defines a 4 component RGBA color for use in the GL Studio DO-178B Runtime Library...
GLclampf _alphaRef
Definition: gls_state_manager.h:247
This header defines any preprocessor defines needed to configure the GL Studio DO-178B Runtime Librar...
void TexEnvi(const GLint param)
void LineWidth(const GLfloat lineWidth)
void EnableAlphaTest(const GlsBool enable)
GLenum _depthFunc
Definition: gls_state_manager.h:255
GLenum _polygonMode
Definition: gls_state_manager.h:272
void EnableLineSmooth(const GlsBool enable)
void EnableBlend(const GlsBool enable)
static const GLuint INVALID_GL_TEXTURE_INDEX
Definition: gls_state_manager.h:74
GLfloat _lineWidth
Definition: gls_state_manager.h:270
GLenum _shadeModel
Definition: gls_state_manager.h:261
void EnableDepthTest(const GlsBool enable)
GLenum _alphaBlendSFactor
Definition: gls_state_manager.h:251
This header defines a GLS_DEBUG only macro for facilitating evaluating class invariants in the GL Stu...
void TexParameteri2D(const GLenum pname, const GLint param)
void EnableTexture2D(const GlsBool enable)
GLenum _alphaFunc
Definition: gls_state_manager.h:246
void Begin(const GLenum mode)
void BlendFunc(const GLenum sfactor, const GLenum dfactor)
void EnableCullFace(const GlsBool enable)
GLuint _textureIndex2D
Definition: gls_state_manager.h:267
virtual ~GlsStateManager()
GlsBool _alphaTest
Definition: gls_state_manager.h:248
GlsBool _texture2D
Definition: gls_state_manager.h:265
void DepthFunc(const GLenum func)
GLenum _alphaBlendDFactor
Definition: gls_state_manager.h:252
GlsBool _cullFace
Definition: gls_state_manager.h:259
GlsBool _alphaBlend
Definition: gls_state_manager.h:250
void End(void)
void AlphaFunc(const GLenum func, const GLclampf ref)
This header includes the API for the OpenGL provider used in the GL Studio DO-178B Runtime Library...