0025147: Visualization, TKOpenGl - support EGL as alternative to GLX
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.hxx
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
16#ifndef _OpenGl_GraphicDriver_HeaderFile
17#define _OpenGl_GraphicDriver_HeaderFile
19#include <Graphic3d_GraphicDriver.hxx>
20#include <Handle_OpenGl_GraphicDriver.hxx>
5e27df78 21#include <OpenGl_Context.hxx>
a174a3c5 22#include <OpenGl_PrinterContext.hxx>
24#include <Standard_CString.hxx>
26#include <Handle_TColStd_HArray1OfByte.hxx>
27#include <Handle_TColStd_HArray1OfReal.hxx>
28#include <Quantity_PlaneAngle.hxx>
29#include <Quantity_NameOfColor.hxx>
bf75be98 30#include <Handle_OpenGl_View.hxx>
1981cb22 31#include <Handle_OpenGl_Workspace.hxx>
33#include <Aspect_Display.hxx>
dc3fe572 34#include <Aspect_DisplayConnection.hxx>
35#include <Aspect_GradientFillMethod.hxx>
36#include <Aspect_FillMethod.hxx>
37#include <Aspect_CLayer2d.hxx>
38#include <Aspect_TypeOfTriedronPosition.hxx>
39#include <Aspect_TypeOfTriedronEcho.hxx>
40#include <Aspect_Handle.hxx>
41#include <Aspect_PrintAlgo.hxx>
938d4544 42#include <gp_Ax2.hxx>
43#include <Graphic3d_CView.hxx>
44#include <Graphic3d_CStructure.hxx>
2166f0fa 45#include <Graphic3d_CPick.hxx>
46#include <Graphic3d_TextPath.hxx>
47#include <Graphic3d_HorizontalTextAlignment.hxx>
48#include <Graphic3d_VerticalTextAlignment.hxx>
49#include <Graphic3d_CUserDraw.hxx>
50#include <Graphic3d_CGraduatedTrihedron.hxx>
51#include <Graphic3d_TypeOfComposition.hxx>
52#include <Graphic3d_ExportFormat.hxx>
53#include <Graphic3d_SortType.hxx>
54#include <Graphic3d_PtrFrameBuffer.hxx>
55#include <Graphic3d_Array1OfVertex.hxx>
2166f0fa 56#include <Graphic3d_Array2OfVertex.hxx>
692613e5 57#include <Graphic3d_BufferType.hxx>
58#include <NCollection_DataMap.hxx>
60class TColStd_Array1OfInteger;
61class TColStd_Array1OfReal;
62class TColStd_Array2OfReal;
63class TColStd_HArray1OfByte;
64class Graphic3d_Vector;
65class Quantity_Color;
66class Graphic3d_Vertex;
67class Aspect_Array1OfEdge;
68class TCollection_ExtendedString;
692613e5 69class Image_PixMap;
2166f0fa 70class TColStd_HArray1OfReal;
2166f0fa 71class Handle(OpenGl_Workspace);
5e27df78 72class OpenGl_Element;
2166f0fa 73class OpenGl_Structure;
a174a3c5 74class OpenGl_Text;
2166f0fa 75
392ac980 76//! Tool class to implement consistent state counter
77//! for objects inside the same driver instance.
78class OpenGl_StateCounter
82 OpenGl_StateCounter() : myCounter (0) { }
84 Standard_Size Increment() { return ++myCounter; }
25b97fac 87
392ac980 88 Standard_Size myCounter;
ebc93ae7 91//! This class defines an OpenGl graphic driver
92class OpenGl_GraphicDriver : public Graphic3d_GraphicDriver
bf75be98 94public:
2166f0fa 95
25b97fac 96 //! Constructor.
97 //! @param theDisp connection to display, required on Linux but optional on other systems
98 //! @param theToStealActiveContext option to use OpenGL context currently bound to the thread rather than creating own one, currently implemented only for Android
99 Standard_EXPORT OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp,
100 const Standard_Boolean theToStealActiveContext = Standard_False);
65993a95 101
2166f0fa 102 Standard_EXPORT Standard_Integer InquireLightLimit ();
2166f0fa 103 Standard_EXPORT Standard_Integer InquireViewLimit ();
2166f0fa 104
105public: // Methods for graphical structures
63bcc448 107 Standard_EXPORT void DisplayStructure (const Graphic3d_CView& theCView,
108 Graphic3d_CStructure& theCStructure,
109 const Standard_Integer thePriority);
110 Standard_EXPORT void EraseStructure (const Graphic3d_CView& theCView,
111 Graphic3d_CStructure& theCStructure);
112 Standard_EXPORT void RemoveStructure (Handle(Graphic3d_CStructure)& theCStructure);
113 Standard_EXPORT Handle(Graphic3d_CStructure) Structure (const Handle(Graphic3d_StructureManager)& theManager);
2166f0fa 114
679ecdee 115 Standard_EXPORT Standard_Boolean SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
116 const Standard_Boolean theDrawToFrontBuffer);
117 Standard_EXPORT void DisplayImmediateStructure (const Graphic3d_CView& theCView,
118 const Graphic3d_CStructure& theCStructure);
119 Standard_EXPORT void EraseImmediateStructure (const Graphic3d_CView& theCView,
120 const Graphic3d_CStructure& theCStructure);
124 Standard_EXPORT void ActivateView (const Graphic3d_CView& ACView);
125 Standard_EXPORT void AntiAliasing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
126 Standard_EXPORT void Background (const Graphic3d_CView& ACView);
127 Standard_EXPORT void GradientBackground (const Graphic3d_CView& ACView, const Quantity_Color& AColor1, const Quantity_Color& AColor2, const Aspect_GradientFillMethod FillStyle);
128 Standard_EXPORT void BackgroundImage (const Standard_CString FileName, const Graphic3d_CView& ACView, const Aspect_FillMethod FillStyle);
129 Standard_EXPORT void SetBgImageStyle (const Graphic3d_CView& ACView, const Aspect_FillMethod FillStyle);
130 Standard_EXPORT void SetBgGradientStyle (const Graphic3d_CView& ACView, const Aspect_GradientFillMethod FillStyle);
131 Standard_EXPORT void ClipLimit (const Graphic3d_CView& ACView, const Standard_Boolean AWait);
132 Standard_EXPORT void DeactivateView (const Graphic3d_CView& ACView);
133 Standard_EXPORT void DepthCueing (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
2166f0fa 134 Standard_EXPORT void RatioWindow (const Graphic3d_CView& ACView);
679ecdee 135 Standard_EXPORT void Redraw (const Graphic3d_CView& theCView,
136 const Aspect_CLayer2d& theCUnderLayer,
137 const Aspect_CLayer2d& theCOverLayer,
138 const Standard_Integer theX = 0,
139 const Standard_Integer theY = 0,
140 const Standard_Integer theWidth = 0,
141 const Standard_Integer theHeight = 0);
142 Standard_EXPORT void RedrawImmediate (const Graphic3d_CView& theCView,
143 const Aspect_CLayer2d& theCUnderLayer,
144 const Aspect_CLayer2d& theCOverLayer);
145 Standard_EXPORT void Invalidate (const Graphic3d_CView& theCView);
146 Standard_EXPORT void RemoveView (const Graphic3d_CView& ACView);
147 Standard_EXPORT void SetLight (const Graphic3d_CView& ACView);
4269bd1b 148 Standard_EXPORT void SetClipPlanes (const Graphic3d_CView& theCView);
b5ac8292 149 Standard_EXPORT void SetCamera (const Graphic3d_CView& theCView);
2166f0fa 150 Standard_EXPORT void SetVisualisation (const Graphic3d_CView& ACView);
2166f0fa 151 Standard_EXPORT void Transparency (const Graphic3d_CView& ACView, const Standard_Boolean AFlag);
2166f0fa 152 Standard_EXPORT Standard_Boolean View (Graphic3d_CView& ACView);
2166f0fa 153 Standard_EXPORT void Environment (const Graphic3d_CView& ACView);
154 Standard_EXPORT void ZBufferTriedronSetup (const Quantity_NameOfColor XColor = Quantity_NOC_RED, const Quantity_NameOfColor YColor = Quantity_NOC_GREEN, const Quantity_NameOfColor ZColor = Quantity_NOC_BLUE1, const Standard_Real SizeRatio = 0.8, const Standard_Real AxisDiametr = 0.05, const Standard_Integer NbFacettes = 12);
155 Standard_EXPORT void TriedronDisplay (const Graphic3d_CView& ACView, const Aspect_TypeOfTriedronPosition APosition = Aspect_TOTP_CENTER, const Quantity_NameOfColor AColor = Quantity_NOC_WHITE, const Standard_Real AScale = 0.02, const Standard_Boolean AsWireframe = Standard_True);
156 Standard_EXPORT void TriedronErase (const Graphic3d_CView& ACView);
157 Standard_EXPORT void TriedronEcho (const Graphic3d_CView& ACView, const Aspect_TypeOfTriedronEcho AType = Aspect_TOTE_NONE);
158 Standard_EXPORT void GraduatedTrihedronDisplay (const Graphic3d_CView& view, const Graphic3d_CGraduatedTrihedron& cubic);
159 Standard_EXPORT void GraduatedTrihedronErase (const Graphic3d_CView& view);
160 Standard_EXPORT void GraduatedTrihedronMinMaxValues (const Standard_ShortReal xmin, const Standard_ShortReal ymin, const Standard_ShortReal zmin, const Standard_ShortReal xmax, const Standard_ShortReal ymax, const Standard_ShortReal zmax);
161 Standard_EXPORT void Layer (Aspect_CLayer2d& ACLayer);
162 Standard_EXPORT void RemoveLayer (const Aspect_CLayer2d& ACLayer);
163 Standard_EXPORT void BeginLayer (const Aspect_CLayer2d& ACLayer);
164 Standard_EXPORT void BeginPolygon2d ();
165 Standard_EXPORT void BeginPolyline2d ();
166 Standard_EXPORT void ClearLayer (const Aspect_CLayer2d& ACLayer);
167 Standard_EXPORT void Draw (const Standard_ShortReal X,const Standard_ShortReal Y);
168 Standard_EXPORT void Edge (const Standard_ShortReal X,const Standard_ShortReal Y);
169 Standard_EXPORT void EndLayer ();
170 Standard_EXPORT void EndPolygon2d ();
171 Standard_EXPORT void EndPolyline2d ();
172 Standard_EXPORT void Move (const Standard_ShortReal X,const Standard_ShortReal Y);
173 Standard_EXPORT void Rectangle (const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal Width,const Standard_ShortReal Height);
174 Standard_EXPORT void SetColor (const Standard_ShortReal R,const Standard_ShortReal G,const Standard_ShortReal B);
175 Standard_EXPORT void SetTransparency (const Standard_ShortReal ATransparency);
176 Standard_EXPORT void UnsetTransparency ();
177 Standard_EXPORT void SetLineAttributes (const Standard_Integer Type,const Standard_ShortReal Width);
25289ec1 178
ebc93ae7 179 //! Set text attributes for under-/overlayer.
180 //! @param theFontName the name of the font to be used
181 //! @param theType the display type of the text
182 //! theR theG theB values define the color of decal or subtitle background
183 //! To set the color of the text you can use the SetColor method.
184 Standard_EXPORT void SetTextAttributes (const Standard_CString theFontName,
185 const Standard_Integer theType,
186 const Standard_ShortReal theR,
187 const Standard_ShortReal theG,
188 const Standard_ShortReal theB);
189 Standard_EXPORT void Text (const Standard_CString AText,const Standard_ShortReal X,const Standard_ShortReal Y,const Standard_ShortReal AHeight);
190 Standard_EXPORT void TextSize (const Standard_CString AText,const Standard_ShortReal AHeight,Standard_ShortReal& AWidth,Standard_ShortReal& AnAscent,Standard_ShortReal& ADescent) const;
191 Standard_EXPORT void SetBackFacingModel (const Graphic3d_CView& aView);
ebc93ae7 192
193 //! Print the contents of all layers of the view to the printer.
194 //! @param thePrinterDC pass the PrinterDeviceContext (HDC)
195 //! @param theToShowBackground when set to FALSE then print the view without background color (background is white) else set to TRUE for printing with current background color
196 //! @param theFileName if != NULL, then the view will be printed to a file
197 //! @param thePrintAlgorithm select print algorithm: stretch, tile
198 //! @param theScaleFactor scaling coefficient, used internally to scale the printings accordingly to the scale factor selected in the printer properties dialog
199 //! @return Standard_True if the data is passed to the printer, otherwise Standard_False if the print operation failed due to the printer errors,
200 //! or lack of system memory. This might be related to insufficient memory or some internal errors.
201 //! All this errors are indicated by the message boxes (on level of OpenGl_GraphicDriver).
202 //! Warning: This function can reuse FBO assigned to the view, please take it into account if you use it for your purposes.
203 Standard_EXPORT Standard_Boolean Print (const Graphic3d_CView& theCView,
204 const Aspect_CLayer2d& theCUnderLayer,
205 const Aspect_CLayer2d& theCOverLayer,
206 const Aspect_Handle thePrinterDC,
207 const Standard_Boolean theToShowBackground,
208 const Standard_CString theFileName,
209 const Aspect_PrintAlgo thePrintAlgorithm = Aspect_PA_STRETCH,
210 const Standard_Real theScaleFactor = 1.0) const;
211 Standard_EXPORT void SetDepthTestEnabled (const Graphic3d_CView& view,const Standard_Boolean isEnabled) const;
212 Standard_EXPORT Standard_Boolean IsDepthTestEnabled (const Graphic3d_CView& view) const;
ebc93ae7 213
214 //! Reads depths of shown pixels of the given rectangle (glReadPixels with GL_DEPTH_COMPONENT)
2166f0fa 215 Standard_EXPORT void ReadDepths (const Graphic3d_CView& view,const Standard_Integer x,const Standard_Integer y,const Standard_Integer width,const Standard_Integer height,const Standard_Address buffer) const;
ebc93ae7 216
217 //! Generate offscreen FBO (needs OpenGL2+ hardware).
218 //! If not supported on hardware returns NULL.
2166f0fa 219 Standard_EXPORT Graphic3d_PtrFrameBuffer FBOCreate (const Graphic3d_CView& view, const Standard_Integer width, const Standard_Integer height);
ebc93ae7 220
221 //! Remove offscreen FBO
2166f0fa 222 Standard_EXPORT void FBORelease (const Graphic3d_CView& view, Graphic3d_PtrFrameBuffer& fboPtr);
ebc93ae7 223
224 //! Dump active rendering buffer into specified memory buffer.
692613e5 225 Standard_EXPORT Standard_Boolean BufferDump (const Graphic3d_CView& theCView,
226 Image_PixMap& theImage,
227 const Graphic3d_BufferType& theBufferType);
ebc93ae7 228
2166f0fa 229 Standard_EXPORT void SetGLLightEnabled (const Graphic3d_CView& view,const Standard_Boolean isEnabled) const;
ebc93ae7 230
2166f0fa 231 Standard_EXPORT Standard_Boolean IsGLLightEnabled (const Graphic3d_CView& view) const;
ebc93ae7 232
2166f0fa 233 Standard_EXPORT Standard_Integer InquirePlaneLimit();
ebc93ae7 234
2166f0fa 235 Standard_EXPORT Standard_ShortReal DefaultTextHeight() const;
ebc93ae7 236
2166f0fa 237 Standard_EXPORT void FBOGetDimensions(const Graphic3d_CView& view,const Graphic3d_PtrFrameBuffer fboPtr,Standard_Integer& width,Standard_Integer& height,Standard_Integer& widthMax,Standard_Integer& heightMax);
ebc93ae7 238
2166f0fa 239 Standard_EXPORT void FBOChangeViewport(const Graphic3d_CView& view,Graphic3d_PtrFrameBuffer& fboPtr,const Standard_Integer width,const Standard_Integer height);
ebc93ae7 240
2166f0fa 241 Standard_EXPORT Standard_Boolean Export(const Standard_CString theFileName,const Graphic3d_ExportFormat theFormat,const Graphic3d_SortType theSortType,const Standard_Integer theWidth,const Standard_Integer theHeight,const Graphic3d_CView& theView,const Aspect_CLayer2d& theLayerUnder,const Aspect_CLayer2d& theLayerOver,const Standard_Real thePrecision = 0.005,const Standard_Address theProgressBarFunc = NULL,const Standard_Address theProgressObject = NULL);
3c3131a0 242
ebc93ae7 243 //! Add a new top-level z layer with ID theLayerId for the view. Z layers allow drawing structures in higher layers in foreground of structures in lower layers.
244 //! To add a structure to desired layer on display it is necessary to set the layer index for the structure.
245 Standard_EXPORT void AddZLayer (const Graphic3d_CView& theCView,
246 const Standard_Integer theLayerId);
248 //! Remove Z layer from the specified view.
249 //! All structures displayed at the moment in layer will be displayed in default layer (the bottom-level z layer).
250 //! To unset layer index from associated structures use method UnsetZLayer (...).
251 Standard_EXPORT void RemoveZLayer (const Graphic3d_CView& theCView,
252 const Standard_Integer theLayerId);
254 //! Unset Z layer ID for all structures.
255 //! The structure indexes will be set to default layer (the bottom-level z layer with ID = 0).
256 Standard_EXPORT void UnsetZLayer (const Standard_Integer theLayerId);
258 //! Change Z layer of a structure.
259 //! The new z layer ID will be used to define the associated layer for structure on display.
260 //! It is recommended to take care of redisplaying the structures already presented in view with previously set layer index.
261 //! This is usually done by viewer manager. Z layers allow drawing structures in higher layers in foreground of structures in lower layers.
262 Standard_EXPORT void ChangeZLayer (const Graphic3d_CStructure& theCStructure,
263 const Standard_Integer theLayerId);
265 //! Change Z layer of a structure already presented in view.
266 //! It is recommended to update z layer of already displayed structures with this method before setting new z layer index to the structure.
267 //! This is usually done by viewer manager.
268 Standard_EXPORT void ChangeZLayer (const Graphic3d_CStructure& theCStructure,
269 const Graphic3d_CView& theCView,
270 const Standard_Integer theNewLayerId);
272 //! Get Z layer ID of the structure. If the structure doesn't exists in graphic driver, the method returns -1.
273 Standard_EXPORT Standard_Integer GetZLayer (const Graphic3d_CStructure& theCStructure) const;
2166f0fa 274
c5751993 275 //! Sets the settings for a single Z layer of specified view.
276 Standard_EXPORT void SetZLayerSettings (const Graphic3d_CView& theCView,
277 const Standard_Integer theLayerId,
278 const Graphic3d_ZLayerSettings& theSettings);
b7cd4ba7 280 //! Changes priority of a structure within its Z layer for the specified view.
281 Standard_EXPORT void ChangePriority (const Graphic3d_CStructure& theCStructure,
282 const Graphic3d_CView& theCView,
283 const Standard_Integer theNewPriority);
58655684 287 //! @return the visualization options
288 inline const OpenGl_Caps& Options() const
289 {
290 return *myCaps.operator->();
291 }
293 //! @return the visualization options
294 inline OpenGl_Caps& ChangeOptions()
295 {
296 return *myCaps.operator->();
297 }
299 //! VBO usage can be forbidden by this method even if it is supported by GL driver.
300 //! Notice that disabling of VBO will cause rendering performance degradation.
301 //! Warning! This method should be called only before any primitives are displayed in GL scene!
302 Standard_EXPORT void EnableVBO (const Standard_Boolean theToTurnOn);
f0430952 304 //! Returns information about GPU memory usage.
305 //! Please read OpenGl_Context::MemoryInfo() for more description.
306 Standard_EXPORT Standard_Boolean MemoryInfo (Standard_Size& theFreeBytes,
307 TCollection_AsciiString& theInfo) const;
5e27df78 309 //! UserDraw function prototype
310 typedef OpenGl_Element* (*OpenGl_UserDrawCallback_t )(const CALL_DEF_USERDRAW* );
2166f0fa 311
5e27df78 312 //! Method to setup UserDraw callback
313 Standard_EXPORT OpenGl_UserDrawCallback_t& UserDrawCallback();
2166f0fa 314
e276548b 315public:
e276548b 316
5e27df78 317 //! Method to retrieve valid GL context.
318 //! Could return NULL-handle if no window created by this driver.
319 Standard_EXPORT const Handle(OpenGl_Context)& GetSharedContext() const;
2166f0fa 320
b7cd4ba7 321 //! Marks BVH tree for given priority list as dirty and
322 //! marks primitive set for rebuild.
323 Standard_EXPORT void InvalidateBVHData (Graphic3d_CView& theCView, const Standard_Integer theLayerId);
25b97fac 325#if defined(HAVE_EGL) || defined(__ANDROID__)
326 Aspect_Display getRawGlDisplay() const { return myEglDisplay; }
327 Aspect_RenderingContext getRawGlContext() const { return myEglContext; }
328 void* getRawGlConfig() const { return myEglConfig; }
333 DEFINE_STANDARD_RTTI(OpenGl_GraphicDriver)
5e27df78 334
25b97fac 337#if defined(HAVE_EGL) || defined(__ANDROID__)
338 Aspect_Display myEglDisplay; //!< EGL connection to the Display : EGLDisplay
339 Aspect_RenderingContext myEglContext; //!< EGL rendering context : EGLContext
340 void* myEglConfig; //!< EGL configuration : EGLConfig
58655684 343 Handle(OpenGl_Caps) myCaps;
5e27df78 344 NCollection_DataMap<Standard_Integer, Handle(OpenGl_View)> myMapOfView;
345 NCollection_DataMap<Standard_Integer, Handle(OpenGl_Workspace)> myMapOfWS;
346 NCollection_DataMap<Standard_Integer, OpenGl_Structure*> myMapOfStructure;
a174a3c5 347 mutable Handle(OpenGl_PrinterContext) myPrintContext;
5e27df78 348 OpenGl_UserDrawCallback_t myUserDrawCallback;
a174a3c5 349 OpenGl_Text* myTempText; //!< variable for compatibility (drawing text in layers)
5e27df78 350
392ac980 351public:
8d3f219f 353 //! State counter for OpenGl structures.
392ac980 354 OpenGl_StateCounter* GetStateCounter() const { return &myStateCounter; }
8d3f219f 356 //! Returns unique ID for primitive arrays.
357 const Standard_Size GetNextPrimitiveArrayUID() const { return myUIDGenerator.Increment(); }
392ac980 359private:
8d3f219f 361 mutable OpenGl_StateCounter myStateCounter; //!< State counter for OpenGl structures.
362 mutable OpenGl_StateCounter myUIDGenerator; //!< Unique ID counter for primitive arrays.
392ac980 363
366#endif //_OpenGl_GraphicDriver_HeaderFile