1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15 
16 #ifndef _OpenGl_GraphicDriver_HeaderFile
17 #define _OpenGl_GraphicDriver_HeaderFile
18 
19 #include <Graphic3d_GraphicDriver.hxx>
20 
21 #include <Aspect_Display.hxx>
22 #include <Aspect_DisplayConnection.hxx>
23 #include <Aspect_RenderingContext.hxx>
24 #include <Graphic3d_CView.hxx>
25 #include <Graphic3d_CStructure.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <OpenGl_Context.hxx>
28 
29 class Aspect_Window;
30 class Quantity_Color;
31 class OpenGl_Structure;
32 class OpenGl_View;
33 class OpenGl_Window;
34 
35 //! Tool class to implement consistent state counter
36 //! for objects inside the same driver instance.
37 class OpenGl_StateCounter
38 {
39 public:
40 
OpenGl_StateCounter()41   OpenGl_StateCounter() : myCounter (0) { }
42 
Increment()43   Standard_Size Increment() { return ++myCounter; }
44 
45 private:
46 
47   Standard_Size myCounter;
48 };
49 
50 //! This class defines an OpenGl graphic driver
51 class OpenGl_GraphicDriver : public Graphic3d_GraphicDriver
52 {
53   DEFINE_STANDARD_RTTIEXT(OpenGl_GraphicDriver, Graphic3d_GraphicDriver)
54 public:
55 
56   //! Constructor.
57   //! @param theDisp connection to display, required on Linux but optional on other systems
58   //! @param theToInitialize perform initialization of default OpenGL context on construction
59   Standard_EXPORT OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
60                                         const Standard_Boolean                  theToInitialize = Standard_True);
61 
62   //! Destructor.
63   Standard_EXPORT virtual ~OpenGl_GraphicDriver();
64 
65   //! Release default context.
66   Standard_EXPORT void ReleaseContext();
67 
68   //! Perform initialization of default OpenGL context.
69   Standard_EXPORT Standard_Boolean InitContext();
70 
71   //! Initialize default OpenGL context using existing one.
72   //! @param theEglDisplay EGL connection to the Display
73   //! @param theEglContext EGL rendering context
74   //! @param theEglConfig  EGL configuration
75   Standard_EXPORT Standard_Boolean InitEglContext (Aspect_Display          theEglDisplay,
76                                                    Aspect_RenderingContext theEglContext,
77                                                    void*                   theEglConfig);
78 
79   //! Request limit of graphic resource of specific type.
80   Standard_EXPORT virtual Standard_Integer InquireLimit (const Graphic3d_TypeOfLimit theType) const Standard_OVERRIDE;
81 
82 public:
83 
84   Standard_EXPORT virtual Handle(Graphic3d_CStructure) CreateStructure (const Handle(Graphic3d_StructureManager)& theManager) Standard_OVERRIDE;
85 
86   Standard_EXPORT virtual void RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure) Standard_OVERRIDE;
87 
88   Standard_EXPORT virtual Handle(Graphic3d_CView) CreateView (const Handle(Graphic3d_StructureManager)& theMgr) Standard_OVERRIDE;
89 
90   Standard_EXPORT virtual void RemoveView (const Handle(Graphic3d_CView)& theView) Standard_OVERRIDE;
91 
92   Standard_EXPORT virtual Handle(OpenGl_Window) CreateRenderWindow (const Handle(Aspect_Window)& theWindow, const Aspect_RenderingContext theContext);
93 
94 public:
95 
96   Standard_EXPORT void TextSize (const Handle(Graphic3d_CView)& theView,
97                                  const Standard_CString         theText,
98                                  const Standard_ShortReal       theHeight,
99                                  Standard_ShortReal&            theWidth,
100                                  Standard_ShortReal&            theAscent,
101                                  Standard_ShortReal&            theDescent) const Standard_OVERRIDE;
102 
103   Standard_EXPORT Standard_ShortReal DefaultTextHeight() const Standard_OVERRIDE;
104 
105   Standard_EXPORT Standard_Boolean ViewExists (const Handle(Aspect_Window)& theWindow, Handle(Graphic3d_CView)& theView) Standard_OVERRIDE;
106 
107 public:
108 
109   //! Adds a layer to all views.
110   //! @param theLayerId    [in] id of new layer, should be > 0 (negative values are reserved for default layers).
111   //! @param theSettings   [in] new layer settings
112   //! @param theLayerAfter [in] id of layer to append new layer before
113   Standard_EXPORT virtual void InsertLayerBefore (const Graphic3d_ZLayerId theNewLayerId,
114                                                   const Graphic3d_ZLayerSettings& theSettings,
115                                                   const Graphic3d_ZLayerId theLayerAfter) Standard_OVERRIDE;
116 
117   //! Adds a layer to all views.
118   //! @param theLayerId     [in] id of created layer
119   //! @param theSettings    [in] new layer settings
120   //! @param theLayerBefore [in] id of layer to append new layer after
121   Standard_EXPORT virtual void InsertLayerAfter (const Graphic3d_ZLayerId theNewLayerId,
122                                                  const Graphic3d_ZLayerSettings& theSettings,
123                                                  const Graphic3d_ZLayerId theLayerBefore) Standard_OVERRIDE;
124 
125   //! Removes Z layer. All structures displayed at the moment in layer will be displayed in
126   //! default layer (the bottom-level z layer). By default, there are always default
127   //! bottom-level layer that can't be removed.  The passed theLayerId should be not less than 0
128   //! (reserved for default layers that can not be removed).
129   Standard_EXPORT void RemoveZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
130 
131   //! Sets the settings for a single Z layer.
132   Standard_EXPORT void SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings) Standard_OVERRIDE;
133 
134 public:
135 
136   //! @return the visualization options
Options() const137   inline const OpenGl_Caps& Options() const
138   {
139     return *myCaps.operator->();
140   }
141 
142   //! @return the visualization options
ChangeOptions()143   inline OpenGl_Caps& ChangeOptions()
144   {
145     return *myCaps.operator->();
146   }
147 
148   //! Specify swap buffer behavior.
149   Standard_EXPORT void SetBuffersNoSwap (const Standard_Boolean theIsNoSwap);
150 
151   //! VBO usage can be forbidden by this method even if it is supported by GL driver.
152   //! Notice that disabling of VBO will cause rendering performance degradation.
153   //! Warning! This method should be called only before any primitives are displayed in GL scene!
154   Standard_EXPORT virtual void EnableVBO (const Standard_Boolean theToTurnOn) Standard_OVERRIDE;
155 
156 
157   //! Returns TRUE if vertical synchronization with display refresh rate (VSync) should be used; TRUE by default.
158   Standard_EXPORT virtual bool IsVerticalSync() const Standard_OVERRIDE;
159 
160   //! Set if vertical synchronization with display refresh rate (VSync) should be used.
161   Standard_EXPORT virtual void SetVerticalSync (bool theToEnable) Standard_OVERRIDE;
162 
163   //! Returns information about GPU memory usage.
164   //! Please read OpenGl_Context::MemoryInfo() for more description.
165   Standard_EXPORT Standard_Boolean MemoryInfo (Standard_Size&           theFreeBytes,
166                                                TCollection_AsciiString& theInfo) const Standard_OVERRIDE;
167 
168 public:
169 
170   //! Method to retrieve valid GL context.
171   //! Could return NULL-handle if no window created by this driver.
172   //! @param theBound if TRUE then currently bound context will be returned,
173   //!                 any context will be returned otherwise
174   Standard_EXPORT const Handle(OpenGl_Context)& GetSharedContext (bool theBound = false) const;
175 
getRawGlDisplay() const176   Aspect_Display          getRawGlDisplay() const { return myEglDisplay; }
getRawGlContext() const177   Aspect_RenderingContext getRawGlContext() const { return myEglContext;  }
getRawGlConfig() const178   void*                   getRawGlConfig()  const { return myEglConfig; }
179 
180   //! Set device lost flag for redrawn views.
181   Standard_EXPORT void setDeviceLost();
182 
183 public:
184 
185   //! State counter for OpenGl structures.
GetStateCounter() const186   OpenGl_StateCounter* GetStateCounter() const { return &myStateCounter; }
187 
188   //! Returns unique ID for primitive arrays.
GetNextPrimitiveArrayUID() const189   Standard_Size GetNextPrimitiveArrayUID() const { return myUIDGenerator.Increment(); }
190 
191 protected:
192 
193   //! Choose default visual for new windows created by Aspect_DisplayConnection.
194   Standard_EXPORT void chooseVisualInfo();
195 
196 protected:
197 
198   Standard_Boolean        myIsOwnContext; //!< indicates that shared context has been created within OpenGl_GraphicDriver
199 
200   Aspect_Display          myEglDisplay;   //!< EGL connection to the Display : EGLDisplay
201   Aspect_RenderingContext myEglContext;   //!< EGL rendering context         : EGLContext
202   void*                   myEglConfig;    //!< EGL configuration             : EGLConfig
203 
204   Handle(OpenGl_Caps)                                      myCaps;
205   NCollection_Map<Handle(OpenGl_View)>                     myMapOfView;
206   NCollection_DataMap<Standard_Integer, OpenGl_Structure*> myMapOfStructure;
207 
208   mutable OpenGl_StateCounter myStateCounter; //!< State counter for OpenGl structures.
209   mutable OpenGl_StateCounter myUIDGenerator; //!< Unique ID counter for primitive arrays.
210 
211 };
212 
213 DEFINE_STANDARD_HANDLE(OpenGl_GraphicDriver, Graphic3d_GraphicDriver)
214 
215 #endif //_OpenGl_GraphicDriver_HeaderFile
216