b92e9e3f604561f50324b83352fce2bc3693d8b7
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.hxx
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_GradientFillMethod.hxx>
24 #include <Aspect_FillMethod.hxx>
25 #include <Aspect_TypeOfTriedronPosition.hxx>
26 #include <Aspect_TypeOfTriedronEcho.hxx>
27 #include <Aspect_Handle.hxx>
28 #include <Aspect_PrintAlgo.hxx>
29 #include <Aspect_RenderingContext.hxx>
30 #include <gp_Ax2.hxx>
31 #include <Graphic3d_CView.hxx>
32 #include <Graphic3d_CStructure.hxx>
33 #include <Graphic3d_TextPath.hxx>
34 #include <Graphic3d_HorizontalTextAlignment.hxx>
35 #include <Graphic3d_VerticalTextAlignment.hxx>
36 #include <Graphic3d_CUserDraw.hxx>
37 #include <Graphic3d_GraduatedTrihedron.hxx>
38 #include <Graphic3d_TypeOfComposition.hxx>
39 #include <Graphic3d_ExportFormat.hxx>
40 #include <Graphic3d_SortType.hxx>
41 #include <Graphic3d_PtrFrameBuffer.hxx>
42 #include <Graphic3d_Array1OfVertex.hxx>
43 #include <Graphic3d_Array2OfVertex.hxx>
44 #include <Graphic3d_BufferType.hxx>
45 #include <NCollection_DataMap.hxx>
46 #include <OpenGl_Context.hxx>
47 #include <OpenGl_MapOfZLayerSettings.hxx>
48 #include <Quantity_PlaneAngle.hxx>
49 #include <Quantity_NameOfColor.hxx>
50 #include <Standard_CString.hxx>
51 #include <TColStd_Array1OfInteger.hxx>
52 #include <TColStd_Array2OfReal.hxx>
53 #include <TColStd_HArray1OfByte.hxx>
54 #include <TColStd_HArray1OfReal.hxx>
55 #include <TColStd_MapOfInteger.hxx>
56
57 class Aspect_Window;
58 class Graphic3d_Vector;
59 class Quantity_Color;
60 class Graphic3d_Vertex;
61 class TCollection_ExtendedString;
62 class Image_PixMap;
63 class OpenGl_Element;
64 class OpenGl_Structure;
65 class OpenGl_Text;
66 class OpenGl_View;
67 class OpenGl_Window;
68
69 //! Tool class to implement consistent state counter
70 //! for objects inside the same driver instance.
71 class OpenGl_StateCounter
72 {
73 public:
74
75   OpenGl_StateCounter() : myCounter (0) { }
76
77   Standard_Size Increment() { return ++myCounter; }
78
79 private:
80
81   Standard_Size myCounter;
82 };
83
84 class OpenGl_GraphicDriver;
85
86 DEFINE_STANDARD_HANDLE(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
87
88 //! This class defines an OpenGl graphic driver
89 class OpenGl_GraphicDriver : public Graphic3d_GraphicDriver
90 {
91 public:
92
93   //! Constructor.
94   //! @param theDisp connection to display, required on Linux but optional on other systems
95   //! @param theToInitialize perform initialization of default OpenGL context on construction
96   Standard_EXPORT OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
97                                         const Standard_Boolean                  theToInitialize = Standard_True);
98
99   //! Destructor.
100   Standard_EXPORT virtual ~OpenGl_GraphicDriver();
101
102   //! Release default context.
103   Standard_EXPORT void ReleaseContext();
104
105   //! Perform initialization of default OpenGL context.
106   Standard_EXPORT Standard_Boolean InitContext();
107
108 #if defined(HAVE_EGL) || defined(__ANDROID__)
109   //! Initialize default OpenGL context using existing one.
110   //! @param theEglDisplay EGL connection to the Display
111   //! @param theEglContext EGL rendering context
112   //! @param theEglConfig  EGL configuration
113   Standard_EXPORT Standard_Boolean InitEglContext (Aspect_Display          theEglDisplay,
114                                                    Aspect_RenderingContext theEglContext,
115                                                    void*                   theEglConfig);
116 #endif
117
118   Standard_EXPORT Standard_Integer InquireLightLimit ();
119   Standard_EXPORT Standard_Integer InquireViewLimit ();
120
121 public:
122
123   Standard_EXPORT virtual Handle(Graphic3d_CStructure) CreateStructure (const Handle(Graphic3d_StructureManager)& theManager) Standard_OVERRIDE;
124
125   Standard_EXPORT virtual void RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure) Standard_OVERRIDE;
126
127   Standard_EXPORT virtual Handle(Graphic3d_CView) CreateView (const Handle(Graphic3d_StructureManager)& theMgr) Standard_OVERRIDE;
128
129   Standard_EXPORT virtual void RemoveView (const Handle(Graphic3d_CView)& theView) Standard_OVERRIDE;
130
131   Standard_EXPORT virtual Handle(OpenGl_Window) CreateRenderWindow (const Handle(Aspect_Window)& theWindow, const Aspect_RenderingContext theContext);
132
133 public:
134
135   Standard_EXPORT void TextSize (const Standard_CString AText,const Standard_ShortReal AHeight,Standard_ShortReal& AWidth,Standard_ShortReal& AnAscent,Standard_ShortReal& ADescent) const;
136
137   Standard_EXPORT Standard_Integer InquirePlaneLimit();
138
139   Standard_EXPORT Standard_ShortReal DefaultTextHeight() const;
140
141   Standard_EXPORT Standard_Boolean ViewExists (const Handle(Aspect_Window)& theWindow, Handle(Graphic3d_CView)& theView) Standard_OVERRIDE;
142
143 public:
144
145   //! Adds a new top-level z layer with ID theLayerId for all views. Z layers allow drawing structures in higher layers
146   //! in foreground of structures in lower layers. To add a structure to desired layer on display it is necessary to
147   //! set the layer index for the structure. The passed theLayerId should be not less than 0 (reserved for default layers).
148   Standard_EXPORT void AddZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
149
150   //! Removes Z layer. All structures displayed at the moment in layer will be displayed in
151   //! default layer (the bottom-level z layer). By default, there are always default
152   //! bottom-level layer that can't be removed.  The passed theLayerId should be not less than 0
153   //! (reserved for default layers that can not be removed).
154   Standard_EXPORT void RemoveZLayer (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
155
156   //! Returns list of Z layers defined for the graphical driver.
157   Standard_EXPORT void ZLayers (TColStd_SequenceOfInteger& theLayerSeq) const Standard_OVERRIDE;
158
159   //! Sets the settings for a single Z layer.
160   Standard_EXPORT void SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, const Graphic3d_ZLayerSettings& theSettings) Standard_OVERRIDE;
161
162   //! Returns the settings of a single Z layer.
163   Standard_EXPORT virtual Graphic3d_ZLayerSettings ZLayerSettings (const Graphic3d_ZLayerId theLayerId) Standard_OVERRIDE;
164
165 public:
166
167   //! @return the visualization options
168   inline const OpenGl_Caps& Options() const
169   {
170     return *myCaps.operator->();
171   }
172
173   //! @return the visualization options
174   inline OpenGl_Caps& ChangeOptions()
175   {
176     return *myCaps.operator->();
177   }
178
179   //! Specify swap buffer behavior.
180   Standard_EXPORT void SetBuffersNoSwap (const Standard_Boolean theIsNoSwap);
181
182   //! VBO usage can be forbidden by this method even if it is supported by GL driver.
183   //! Notice that disabling of VBO will cause rendering performance degradation.
184   //! Warning! This method should be called only before any primitives are displayed in GL scene!
185   Standard_EXPORT void EnableVBO (const Standard_Boolean theToTurnOn);
186
187   //! Returns information about GPU memory usage.
188   //! Please read OpenGl_Context::MemoryInfo() for more description.
189   Standard_EXPORT Standard_Boolean MemoryInfo (Standard_Size&           theFreeBytes,
190                                                TCollection_AsciiString& theInfo) const;
191
192   //! UserDraw function prototype
193   typedef OpenGl_Element* (*OpenGl_UserDrawCallback_t )(const CALL_DEF_USERDRAW* );
194
195   //! Method to setup UserDraw callback
196   Standard_EXPORT OpenGl_UserDrawCallback_t& UserDrawCallback();
197
198 public:
199
200   //! Method to retrieve valid GL context.
201   //! Could return NULL-handle if no window created by this driver.
202   Standard_EXPORT const Handle(OpenGl_Context)& GetSharedContext() const;
203
204 #if defined(HAVE_EGL) || defined(__ANDROID__)
205   Aspect_Display          getRawGlDisplay() const { return myEglDisplay; }
206   Aspect_RenderingContext getRawGlContext() const { return myEglContext;  }
207   void*                   getRawGlConfig()  const { return myEglConfig; }
208 #endif
209
210 public:
211
212   DEFINE_STANDARD_RTTI(OpenGl_GraphicDriver, Graphic3d_GraphicDriver)
213
214 protected:
215
216   Standard_Boolean        myIsOwnContext; //!< indicates that shared context has been created within OpenGl_GraphicDriver
217 #if defined(HAVE_EGL) || defined(__ANDROID__)
218   Aspect_Display          myEglDisplay;   //!< EGL connection to the Display : EGLDisplay
219   Aspect_RenderingContext myEglContext;   //!< EGL rendering context         : EGLContext
220   void*                   myEglConfig;    //!< EGL configuration             : EGLConfig
221 #endif
222
223   Handle(OpenGl_Caps)                                             myCaps;
224   NCollection_Map<Handle(OpenGl_View)>                            myMapOfView;
225   NCollection_DataMap<Standard_Integer, OpenGl_Structure*>        myMapOfStructure;
226   OpenGl_UserDrawCallback_t                                       myUserDrawCallback;
227
228 public:
229
230   TColStd_MapOfInteger       myLayerIds;
231   TColStd_SequenceOfInteger  myLayerSeq;
232   OpenGl_MapOfZLayerSettings myMapOfZLayerSettings;
233
234 public:
235
236   //! State counter for OpenGl structures.
237   OpenGl_StateCounter* GetStateCounter() const { return &myStateCounter; }
238
239   //! Returns unique ID for primitive arrays.
240   const Standard_Size GetNextPrimitiveArrayUID() const { return myUIDGenerator.Increment(); }
241
242 protected:
243
244   mutable OpenGl_StateCounter myStateCounter; //!< State counter for OpenGl structures.
245   mutable OpenGl_StateCounter myUIDGenerator; //!< Unique ID counter for primitive arrays.
246
247 };
248
249 #endif //_OpenGl_GraphicDriver_HeaderFile