1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_GraphicDriver_HeaderFile
17 #define _OpenGl_GraphicDriver_HeaderFile
19 #include <Graphic3d_GraphicDriver.hxx>
21 #include <Aspect_Display.hxx>
22 #include <Aspect_DisplayConnection.hxx>
23 #include <Aspect_GradientFillMethod.hxx>
24 #include <Aspect_FillMethod.hxx>
25 #include <Aspect_TypeOfTriedronPosition.hxx>
26 #include <Aspect_Handle.hxx>
27 #include <Aspect_RenderingContext.hxx>
29 #include <Graphic3d_CView.hxx>
30 #include <Graphic3d_CStructure.hxx>
31 #include <Graphic3d_TextPath.hxx>
32 #include <Graphic3d_HorizontalTextAlignment.hxx>
33 #include <Graphic3d_VerticalTextAlignment.hxx>
34 #include <Graphic3d_GraduatedTrihedron.hxx>
35 #include <Graphic3d_TypeOfComposition.hxx>
36 #include <Graphic3d_ExportFormat.hxx>
37 #include <Graphic3d_SortType.hxx>
38 #include <Graphic3d_BufferType.hxx>
39 #include <NCollection_DataMap.hxx>
40 #include <OpenGl_Context.hxx>
41 #include <OpenGl_MapOfZLayerSettings.hxx>
42 #include <Quantity_NameOfColor.hxx>
43 #include <Standard_CString.hxx>
44 #include <TColStd_Array1OfInteger.hxx>
45 #include <TColStd_Array2OfReal.hxx>
46 #include <TColStd_HArray1OfByte.hxx>
47 #include <TColStd_HArray1OfReal.hxx>
48 #include <TColStd_MapOfInteger.hxx>
51 class Graphic3d_Vector;
53 class Graphic3d_Vertex;
54 class TCollection_ExtendedString;
57 class OpenGl_Structure;
62 //! Tool class to implement consistent state counter
63 //! for objects inside the same driver instance.
64 class OpenGl_StateCounter
68 OpenGl_StateCounter() : myCounter (0) { }
70 Standard_Size Increment() { return ++myCounter; }
74 Standard_Size myCounter;
77 //! This class defines an OpenGl graphic driver
78 class OpenGl_GraphicDriver : public Graphic3d_GraphicDriver
80 DEFINE_STANDARD_RTTIEXT(OpenGl_GraphicDriver, Graphic3d_GraphicDriver)
84 //! @param theDisp connection to display, required on Linux but optional on other systems
85 //! @param theToInitialize perform initialization of default OpenGL context on construction
86 Standard_EXPORT OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
87 const Standard_Boolean theToInitialize = Standard_True);
90 Standard_EXPORT virtual ~OpenGl_GraphicDriver();
92 //! Release default context.
93 Standard_EXPORT void ReleaseContext();
95 //! Perform initialization of default OpenGL context.
96 Standard_EXPORT Standard_Boolean InitContext();
98 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
99 //! Initialize default OpenGL context using existing one.
100 //! @param theEglDisplay EGL connection to the Display
101 //! @param theEglContext EGL rendering context
102 //! @param theEglConfig EGL configuration
103 Standard_EXPORT Standard_Boolean InitEglContext (Aspect_Display theEglDisplay,
104 Aspect_RenderingContext theEglContext,
108 //! Request limit of graphic resource of specific type.
109 Standard_EXPORT virtual Standard_Integer InquireLimit (const Graphic3d_TypeOfLimit theType) const Standard_OVERRIDE;
113 Standard_EXPORT virtual Handle(Graphic3d_CStructure) CreateStructure (const Handle(Graphic3d_StructureManager)& theManager) Standard_OVERRIDE;
115 Standard_EXPORT virtual void RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure) Standard_OVERRIDE;
117 Standard_EXPORT virtual Handle(Graphic3d_CView) CreateView (const Handle(Graphic3d_StructureManager)& theMgr) Standard_OVERRIDE;
119 Standard_EXPORT virtual void RemoveView (const Handle(Graphic3d_CView)& theView) Standard_OVERRIDE;
121 Standard_EXPORT virtual Handle(OpenGl_Window) CreateRenderWindow (const Handle(Aspect_Window)& theWindow, const Aspect_RenderingContext theContext);
125 Standard_EXPORT void TextSize (const Handle(Graphic3d_CView)& theView,
126 const Standard_CString theText,
127 const Standard_ShortReal theHeight,
128 Standard_ShortReal& theWidth,
129 Standard_ShortReal& theAscent,
130 Standard_ShortReal& theDescent) const Standard_OVERRIDE;
132 Standard_EXPORT Standard_ShortReal DefaultTextHeight() const Standard_OVERRIDE;
134 Standard_EXPORT Standard_Boolean ViewExists (const Handle(Aspect_Window)& theWindow, Handle(Graphic3d_CView)& theView) Standard_OVERRIDE;
138 //! Adds a new top-level z layer with ID theLayerId for all views. Z layers allow drawing structures in higher layers
139 //! in foreground of structures in lower layers. To add a structure to desired layer on display it is necessary to
140 //! set the layer index for the structure. The passed theLayerId should be not less than 0 (reserved for default layers).
141 Standard_EXPORT void AddZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
143 //! Removes Z layer. All structures displayed at the moment in layer will be displayed in
144 //! default layer (the bottom-level z layer). By default, there are always default
145 //! bottom-level layer that can't be removed. The passed theLayerId should be not less than 0
146 //! (reserved for default layers that can not be removed).
147 Standard_EXPORT void RemoveZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
149 //! Returns list of Z layers defined for the graphical driver.
150 Standard_EXPORT void ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const Standard_OVERRIDE;
152 //! Sets the settings for a single Z layer.
153 Standard_EXPORT void SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings) Standard_OVERRIDE;
155 //! Returns the settings of a single Z layer.
156 Standard_EXPORT virtual const Graphic3d_ZLayerSettings& ZLayerSettings (const Graphic3d_ZLayerId theLayerId) const Standard_OVERRIDE;
160 //! @return the visualization options
161 inline const OpenGl_Caps& Options() const
163 return *myCaps.operator->();
166 //! @return the visualization options
167 inline OpenGl_Caps& ChangeOptions()
169 return *myCaps.operator->();
172 //! Specify swap buffer behavior.
173 Standard_EXPORT void SetBuffersNoSwap (const Standard_Boolean theIsNoSwap);
175 //! VBO usage can be forbidden by this method even if it is supported by GL driver.
176 //! Notice that disabling of VBO will cause rendering performance degradation.
177 //! Warning! This method should be called only before any primitives are displayed in GL scene!
178 Standard_EXPORT void EnableVBO (const Standard_Boolean theToTurnOn) Standard_OVERRIDE;
180 //! Returns information about GPU memory usage.
181 //! Please read OpenGl_Context::MemoryInfo() for more description.
182 Standard_EXPORT Standard_Boolean MemoryInfo (Standard_Size& theFreeBytes,
183 TCollection_AsciiString& theInfo) const Standard_OVERRIDE;
187 //! Method to retrieve valid GL context.
188 //! Could return NULL-handle if no window created by this driver.
189 Standard_EXPORT const Handle(OpenGl_Context)& GetSharedContext() const;
191 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
192 Aspect_Display getRawGlDisplay() const { return myEglDisplay; }
193 Aspect_RenderingContext getRawGlContext() const { return myEglContext; }
194 void* getRawGlConfig() const { return myEglConfig; }
197 //! Insert index layer at proper position.
198 Standard_EXPORT void addZLayerIndex (const Graphic3d_ZLayerId theLayerId);
200 //! Set device lost flag for redrawn views.
201 Standard_EXPORT void setDeviceLost();
205 //! State counter for OpenGl structures.
206 OpenGl_StateCounter* GetStateCounter() const { return &myStateCounter; }
208 //! Returns unique ID for primitive arrays.
209 Standard_Size GetNextPrimitiveArrayUID() const { return myUIDGenerator.Increment(); }
213 Standard_Boolean myIsOwnContext; //!< indicates that shared context has been created within OpenGl_GraphicDriver
214 #if defined(HAVE_EGL) || defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__)
215 Aspect_Display myEglDisplay; //!< EGL connection to the Display : EGLDisplay
216 Aspect_RenderingContext myEglContext; //!< EGL rendering context : EGLContext
217 void* myEglConfig; //!< EGL configuration : EGLConfig
220 Handle(OpenGl_Caps) myCaps;
221 NCollection_Map<Handle(OpenGl_View)> myMapOfView;
222 NCollection_DataMap<Standard_Integer, OpenGl_Structure*> myMapOfStructure;
224 TColStd_MapOfInteger myLayerIds;
225 TColStd_SequenceOfInteger myLayerSeq;
226 OpenGl_MapOfZLayerSettings myMapOfZLayerSettings;
228 mutable OpenGl_StateCounter myStateCounter; //!< State counter for OpenGl structures.
229 mutable OpenGl_StateCounter myUIDGenerator; //!< Unique ID counter for primitive arrays.
233 DEFINE_STANDARD_HANDLE(OpenGl_GraphicDriver, Graphic3d_GraphicDriver)
235 #endif //_OpenGl_GraphicDriver_HeaderFile